From b4e228450fc9c85505e4ad98376046b98c08dd80 Mon Sep 17 00:00:00 2001 From: EYHN Date: Thu, 19 Sep 2024 20:25:18 +0800 Subject: [PATCH] fix(core): fix web login --- .../core/src/components/affine/auth/index.tsx | 43 ------------------- .../core/src/modules/cloud/services/auth.ts | 40 ++++++++++++++--- 2 files changed, 35 insertions(+), 48 deletions(-) diff --git a/packages/frontend/core/src/components/affine/auth/index.tsx b/packages/frontend/core/src/components/affine/auth/index.tsx index 6a4d106daa2f..bf4a2642fea9 100644 --- a/packages/frontend/core/src/components/affine/auth/index.tsx +++ b/packages/frontend/core/src/components/affine/auth/index.tsx @@ -1,9 +1,7 @@ import { notify } from '@affine/component'; import { AuthModal as AuthModalBase } from '@affine/component/auth-components'; import { authAtom, type AuthAtomData } from '@affine/core/components/atoms'; -import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks'; import { AuthService } from '@affine/core/modules/cloud'; -import { apis, events } from '@affine/electron-api'; import { useI18n } from '@affine/i18n'; import { useLiveData, useService } from '@toeverything/infra'; import { useAtom } from 'jotai/react'; @@ -45,9 +43,7 @@ const config: { }; export function AuthModal() { - const t = useI18n(); const [authAtomValue, setAuthAtom] = useAtom(authAtom); - const authService = useService(AuthService); const setOpen = useCallback( (open: boolean) => { setAuthAtom(prev => ({ ...prev, openModal: open })); @@ -55,45 +51,6 @@ export function AuthModal() { [setAuthAtom] ); - const signIn = useAsyncCallback( - async ({ - method, - payload, - }: { - method: 'magic-link' | 'oauth'; - payload: any; - }) => { - if (!(await apis?.ui.isActiveTab())) { - return; - } - try { - switch (method) { - case 'magic-link': { - const { email, token } = payload; - await authService.signInMagicLink(email, token); - break; - } - case 'oauth': { - const { code, state, provider } = payload; - await authService.signInOauth(code, state, provider); - break; - } - } - authService.session.revalidate(); - } catch (e) { - notify.error({ - title: t['com.affine.auth.toast.title.failed'](), - message: (e as any).message, - }); - } - }, - [authService, t] - ); - - useEffect(() => { - return events?.ui.onAuthenticationRequest(signIn); - }, [signIn]); - return ( diff --git a/packages/frontend/core/src/modules/cloud/services/auth.ts b/packages/frontend/core/src/modules/cloud/services/auth.ts index 5e72edc32083..00616b9a6a2f 100644 --- a/packages/frontend/core/src/modules/cloud/services/auth.ts +++ b/packages/frontend/core/src/modules/cloud/services/auth.ts @@ -1,5 +1,7 @@ -import { appInfo } from '@affine/electron-api'; +import { notify } from '@affine/component'; +import { apis, appInfo, events } from '@affine/electron-api'; import type { OAuthProviderType } from '@affine/graphql'; +import { I18n } from '@affine/i18n'; import { track } from '@affine/track'; import { ApplicationFocused, @@ -56,6 +58,33 @@ export class AuthService extends Service { private onApplicationStart() { this.session.revalidate(); + + if (BUILD_CONFIG.isElectron) { + events?.ui.onAuthenticationRequest(({ method, payload }) => { + (async () => { + if (!(await apis?.ui.isActiveTab())) { + return; + } + switch (method) { + case 'magic-link': { + const { email, token } = payload; + await this.signInMagicLink(email, token); + break; + } + case 'oauth': { + const { code, state, provider } = payload; + await this.signInOauth(code, state, provider); + break; + } + } + })().catch(e => { + notify.error({ + title: I18n['com.affine.auth.toast.title.failed'](), + message: (e as any).message, + }); + }); + }); + } } private onApplicationFocused() { @@ -97,6 +126,8 @@ export class AuthService extends Service { }, body: JSON.stringify({ email, token }), }); + + this.session.revalidate(); track.$.$.auth.signedIn({ method: 'magic-link' }); } catch (e) { track.$.$.auth.signInFail({ method: 'magic-link' }); @@ -151,6 +182,8 @@ export class AuthService extends Service { }, }); + this.session.revalidate(); + track.$.$.auth.signedIn({ method: 'oauth', provider }); return res.json(); } catch (e) { @@ -167,7 +200,7 @@ export class AuthService extends Service { }) { track.$.$.auth.signIn({ method: 'password' }); try { - const res = await this.fetchService.fetch('/api/auth/sign-in', { + await this.fetchService.fetch('/api/auth/sign-in', { method: 'POST', body: JSON.stringify(credential), headers: { @@ -175,9 +208,6 @@ export class AuthService extends Service { ...this.captchaHeaders(credential.verifyToken, credential.challenge), }, }); - if (!res.ok) { - throw new Error('Failed to sign in'); - } this.session.revalidate(); track.$.$.auth.signedIn({ method: 'password' }); } catch (e) {