diff --git a/packages/backend/server/src/plugins/oauth/controller.ts b/packages/backend/server/src/plugins/oauth/controller.ts index 999c34ef3df40..d553aedf22f3f 100644 --- a/packages/backend/server/src/plugins/oauth/controller.ts +++ b/packages/backend/server/src/plugins/oauth/controller.ts @@ -111,7 +111,6 @@ export class OAuthController { await this.auth.setCookies(req, res, user.id); res.send({ id: user.id, - /* @deprecated */ redirectUri: state.redirectUri, }); } diff --git a/packages/frontend/core/src/components/affine/auth/after-sign-in-send-email.tsx b/packages/frontend/core/src/components/affine/auth/after-sign-in-send-email.tsx index 99916c64246f6..d2e4b68d41832 100644 --- a/packages/frontend/core/src/components/affine/auth/after-sign-in-send-email.tsx +++ b/packages/frontend/core/src/components/affine/auth/after-sign-in-send-email.tsx @@ -19,6 +19,7 @@ import { Captcha, useCaptcha } from './use-captcha'; export const AfterSignInSendEmail = ({ setAuthData: setAuth, email, + redirectUrl, }: AuthPanelProps<'afterSignInSendEmail'>) => { const [resendCountDown, setResendCountDown] = useState(60); @@ -44,7 +45,12 @@ export const AfterSignInSendEmail = ({ try { if (verifyToken) { setResendCountDown(60); - await authService.sendEmailMagicLink(email, verifyToken, challenge); + await authService.sendEmailMagicLink( + email, + verifyToken, + challenge, + redirectUrl + ); } } catch (err) { console.error(err); @@ -53,7 +59,7 @@ export const AfterSignInSendEmail = ({ }); } setIsSending(false); - }, [authService, challenge, email, verifyToken]); + }, [authService, challenge, email, redirectUrl, verifyToken]); const onSignInWithPasswordClick = useCallback(() => { setAuth({ state: 'signInWithPassword' }); diff --git a/packages/frontend/core/src/components/affine/auth/after-sign-up-send-email.tsx b/packages/frontend/core/src/components/affine/auth/after-sign-up-send-email.tsx index be1e0c0b96c65..04c9c257a2a06 100644 --- a/packages/frontend/core/src/components/affine/auth/after-sign-up-send-email.tsx +++ b/packages/frontend/core/src/components/affine/auth/after-sign-up-send-email.tsx @@ -19,7 +19,7 @@ import { Captcha, useCaptcha } from './use-captcha'; export const AfterSignUpSendEmail: FC< AuthPanelProps<'afterSignUpSendEmail'> -> = ({ setAuthData, email }) => { +> = ({ setAuthData, email, redirectUrl }) => { const [resendCountDown, setResendCountDown] = useState(60); useEffect(() => { @@ -42,7 +42,12 @@ export const AfterSignUpSendEmail: FC< setIsSending(true); try { if (verifyToken) { - await authService.sendEmailMagicLink(email, verifyToken, challenge); + await authService.sendEmailMagicLink( + email, + verifyToken, + challenge, + redirectUrl + ); } setResendCountDown(60); } catch (err) { @@ -52,7 +57,7 @@ export const AfterSignUpSendEmail: FC< }); } setIsSending(false); - }, [authService, challenge, email, verifyToken]); + }, [authService, challenge, email, redirectUrl, verifyToken]); return ( <> diff --git a/packages/frontend/core/src/components/affine/auth/index.tsx b/packages/frontend/core/src/components/affine/auth/index.tsx index bf4a2642fea9a..b6ec7edfe2ae5 100644 --- a/packages/frontend/core/src/components/affine/auth/index.tsx +++ b/packages/frontend/core/src/components/affine/auth/index.tsx @@ -30,6 +30,7 @@ export type AuthPanelProps = { updates: { state: T } & Difference, AuthAtomType> ) => void; onSkip?: () => void; + redirectUrl?: string; } & Extract; const config: { @@ -58,7 +59,13 @@ export function AuthModal() { ); } -export function AuthPanel({ onSkip }: { onSkip?: () => void }) { +export function AuthPanel({ + onSkip, + redirectUrl, +}: { + onSkip?: () => void; + redirectUrl?: string | null; +}) { const t = useI18n(); const [authAtomValue, setAuthAtom] = useAtom(authAtom); const authService = useService(AuthService); @@ -98,6 +105,7 @@ export function AuthPanel({ onSkip }: { onSkip?: () => void }) { const props = { ...authAtomValue, onSkip, + redirectUrl, setAuthData, }; diff --git a/packages/frontend/core/src/components/affine/auth/oauth.tsx b/packages/frontend/core/src/components/affine/auth/oauth.tsx index 8f765678ae75d..f464e160edf3f 100644 --- a/packages/frontend/core/src/components/affine/auth/oauth.tsx +++ b/packages/frontend/core/src/components/affine/auth/oauth.tsx @@ -29,7 +29,7 @@ const OAuthProviderMap: Record< }, }; -export function OAuth() { +export function OAuth({ redirectUrl }: { redirectUrl?: string }) { const serverConfig = useService(ServerConfigService).serverConfig; const oauth = useLiveData(serverConfig.features$.map(r => r?.oauth)); const oauthProviders = useLiveData( @@ -41,25 +41,43 @@ export function OAuth() { } return oauthProviders?.map(provider => ( - + )); } -function OAuthProvider({ provider }: { provider: OAuthProviderType }) { +function OAuthProvider({ + provider, + redirectUrl, +}: { + provider: OAuthProviderType; + redirectUrl?: string; +}) { const { icon } = OAuthProviderMap[provider]; const onClick = useCallback(() => { - let oauthUrl = - (BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS || BUILD_CONFIG.isAndroid - ? BUILD_CONFIG.serverUrlPrefix - : '') + `/oauth/login?provider=${provider}`; + const params = new URLSearchParams(); - if (BUILD_CONFIG.isElectron) { - oauthUrl += `&client=${appInfo?.schema}`; + params.set('provider', provider); + + if (redirectUrl) { + params.set('redirect_uri', redirectUrl); } + if (BUILD_CONFIG.isElectron && appInfo) { + params.set('client', appInfo.schema); + } + + const oauthUrl = + (BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS || BUILD_CONFIG.isAndroid + ? BUILD_CONFIG.serverUrlPrefix + : '') + `/oauth/login?${params.toString()}`; + popupWindow(oauthUrl); - }, [provider]); + }, [provider, redirectUrl]); return (