mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
fix(core): fix web login (#8312)
This commit is contained in:
@@ -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 (
|
||||
<AuthModalBase open={authAtomValue.openModal} setOpen={setOpen}>
|
||||
<AuthPanel />
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user