Skip to content

Commit bffc294

Browse files
committed
fix(core): oauth popup blocked in safari (#8144)
fix(core): oauth popup blocked in safari fix(core): standarize auth routes fix AF-1352
1 parent be4df0f commit bffc294

File tree

8 files changed

+64
-50
lines changed

8 files changed

+64
-50
lines changed

packages/frontend/core/public/robots.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Disallow: /oauth/*
66
Disallow: /workspace/*
77
Disallow: /public-workspace/*
88
Disallow: /auth/*
9-
Disallow: /signin/*
9+
Disallow: /sign-in
1010
Disallow: /_next/*
1111
Disallow: /invite/*
1212
Disallow: /*/invite/*
Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { notify, Skeleton } from '@affine/component';
1+
import { Skeleton } from '@affine/component';
22
import { Button } from '@affine/component/ui/button';
3-
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
4-
import { track } from '@affine/core/mixpanel';
5-
import { popupWindow } from '@affine/core/utils';
6-
import { apis } from '@affine/electron-api';
73
import { OAuthProviderType } from '@affine/graphql';
84
import { GithubIcon, GoogleDuotoneIcon } from '@blocksuite/icons/rc';
95
import { useLiveData, useService } from '@toeverything/infra';
106
import type { ReactElement } from 'react';
11-
import { useState } from 'react';
127

13-
import { AuthService, ServerConfigService } from '../../../modules/cloud';
8+
import { ServerConfigService } from '../../../modules/cloud';
149

1510
const OAuthProviderMap: Record<
1611
OAuthProviderType,
@@ -50,39 +45,23 @@ export function OAuth() {
5045

5146
function OAuthProvider({ provider }: { provider: OAuthProviderType }) {
5247
const { icon } = OAuthProviderMap[provider];
53-
const authService = useService(AuthService);
54-
const [isConnecting, setIsConnecting] = useState(false);
55-
56-
const onClick = useAsyncCallback(async () => {
57-
try {
58-
setIsConnecting(true);
59-
const url = await authService.oauthPreflight(provider);
60-
if (environment.isElectron) {
61-
await apis?.ui.openExternal(url);
62-
} else {
63-
popupWindow(url);
64-
}
65-
} catch (err) {
66-
console.error(err);
67-
notify.error({ title: 'Failed to sign in, please try again.' });
68-
} finally {
69-
setIsConnecting(false);
70-
track.$.$.auth.oauth({ provider });
71-
}
72-
}, [authService, provider]);
7348

7449
return (
75-
<Button
76-
key={provider}
77-
variant="primary"
78-
block
79-
size="extraLarge"
80-
style={{ marginTop: 30, width: '100%' }}
81-
prefix={icon}
82-
onClick={onClick}
50+
<a
51+
href={`/oauth/login?provider=${provider}`}
52+
target="_blank"
53+
rel="noreferrer"
8354
>
84-
Continue with {provider}
85-
{isConnecting && '...'}
86-
</Button>
55+
<Button
56+
key={provider}
57+
variant="primary"
58+
block
59+
size="extraLarge"
60+
style={{ marginTop: 30, width: '100%' }}
61+
prefix={icon}
62+
>
63+
Continue with {provider}
64+
</Button>
65+
</a>
8766
);
8867
}

packages/frontend/core/src/hooks/use-navigate-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function useNavigateHelper() {
163163
}
164164

165165
return navigate(
166-
'/signIn' +
166+
'/sign-in' +
167167
(searchParams.toString() ? '?' + searchParams.toString() : ''),
168168
{
169169
replace: logic === RouteLogic.REPLACE,

packages/frontend/core/src/pages/auth/magic-link.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const loader: LoaderFunction = ({ request }) => {
2525
const redirectUri = params.get('redirect_uri');
2626

2727
if (!email || !token) {
28-
return redirect('/signIn?error=Invalid magic link');
28+
return redirect('/sign-in?error=Invalid magic link');
2929
}
3030

3131
const payload: LoaderData = {
@@ -61,7 +61,7 @@ export const Component = () => {
6161
nav(data.redirectUri ?? '/');
6262
})
6363
.catch(e => {
64-
nav(`/signIn?error=${encodeURIComponent(e.message)}`);
64+
nav(`/sign-in?error=${encodeURIComponent(e.message)}`);
6565
});
6666
}, [data, auth, nav]);
6767

packages/frontend/core/src/pages/auth/oauth-callback.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const loader: LoaderFunction = async ({ request }) => {
2222
let stateStr = queries.get('state') ?? '{}';
2323

2424
if (!code || !stateStr) {
25-
return redirect('/signIn?error=Invalid oauth callback parameters');
25+
return redirect('/sign-in?error=Invalid oauth callback parameters');
2626
}
2727

2828
try {
@@ -46,7 +46,7 @@ export const loader: LoaderFunction = async ({ request }) => {
4646
`/open-app/url?url=${encodeURIComponent(`${client}://authentication?${authParams.toString()}`)}`
4747
);
4848
} catch {
49-
return redirect('/signIn?error=Invalid oauth callback parameters');
49+
return redirect('/sign-in?error=Invalid oauth callback parameters');
5050
}
5151
};
5252

@@ -64,7 +64,7 @@ export const Component = () => {
6464
nav(redirectUri ?? '/');
6565
})
6666
.catch(e => {
67-
nav(`/signIn?error=${encodeURIComponent(e.message)}`);
67+
nav(`/sign-in?error=${encodeURIComponent(e.message)}`);
6868
});
6969
}, [data, auth, nav]);
7070

packages/frontend/core/src/pages/auth/desktop-signin.tsx renamed to packages/frontend/core/src/pages/auth/oauth-login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const loader: LoaderFunction = async ({ request }) => {
3636
}
3737

3838
return redirect(
39-
`/signIn?error=${encodeURIComponent(`Invalid oauth provider ${provider}`)}`
39+
`/sign-in?error=${encodeURIComponent(`Invalid oauth provider ${provider}`)}`
4040
);
4141
};
4242

@@ -54,7 +54,7 @@ export const Component = () => {
5454
location.href = url;
5555
})
5656
.catch(e => {
57-
nav(`/signIn?error=${encodeURIComponent(e.message)}`);
57+
nav(`/sign-in?error=${encodeURIComponent(e.message)}`);
5858
});
5959
}, [data, auth, nav]);
6060

packages/frontend/core/src/router.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const topLevelRoutes = [
8282
path: '/try-cloud',
8383
loader: () => {
8484
return redirect(
85-
`/signIn?redirect_uri=${encodeURIComponent('/?initCloud=true')}`
85+
`/sign-in?redirect_uri=${encodeURIComponent('/?initCloud=true')}`
8686
);
8787
},
8888
},
@@ -99,7 +99,7 @@ export const topLevelRoutes = [
9999
lazy: () => import(/* webpackChunkName: "auth" */ './pages/auth/auth'),
100100
},
101101
{
102-
path: '/signIn',
102+
path: '/sign-In',
103103
lazy: () =>
104104
import(/* webpackChunkName: "auth" */ './pages/auth/sign-in'),
105105
},
@@ -108,6 +108,11 @@ export const topLevelRoutes = [
108108
lazy: () =>
109109
import(/* webpackChunkName: "auth" */ './pages/auth/magic-link'),
110110
},
111+
{
112+
path: '/oauth/login',
113+
lazy: () =>
114+
import(/* webpackChunkName: "auth" */ './pages/auth/oauth-login'),
115+
},
111116
{
112117
path: '/oauth/callback',
113118
lazy: () =>
@@ -117,7 +122,16 @@ export const topLevelRoutes = [
117122
// TODO(@forehalo): remove
118123
{
119124
path: '/desktop-signin',
120-
lazy: () => import('./pages/auth/desktop-signin'),
125+
lazy: () =>
126+
import(/* webpackChunkName: "auth" */ './pages/auth/oauth-login'),
127+
},
128+
// deprecated, keep for old client compatibility
129+
// use '/sign-in'
130+
// TODO(@forehalo): remove
131+
{
132+
path: '/signIn',
133+
lazy: () =>
134+
import(/* webpackChunkName: "auth" */ './pages/auth/sign-in'),
121135
},
122136
{
123137
path: '/open-app/:action',

packages/frontend/mobile/src/router.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ export const topLevelRoutes = [
6666
path: '/sign-in',
6767
lazy: () => import('./pages/sign-in'),
6868
},
69+
{
70+
path: '/magic-link',
71+
lazy: () =>
72+
import(
73+
/* webpackChunkName: "auth" */ '@affine/core/pages/auth/magic-link'
74+
),
75+
},
76+
{
77+
path: '/oauth/login',
78+
lazy: () =>
79+
import(
80+
/* webpackChunkName: "auth" */ '@affine/core/pages/auth/oauth-login'
81+
),
82+
},
83+
{
84+
path: '/oauth/callback',
85+
lazy: () =>
86+
import(
87+
/* webpackChunkName: "auth" */ '@affine/core/pages/auth/oauth-callback'
88+
),
89+
},
6990
{
7091
path: '/redirect-proxy',
7192
lazy: () => import('@affine/core/pages/redirect'),

0 commit comments

Comments
 (0)