mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-13 12:54:59 +08:00
fix(core): fix web login (#8312)
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
import { notify } from '@affine/component';
|
import { notify } from '@affine/component';
|
||||||
import { AuthModal as AuthModalBase } from '@affine/component/auth-components';
|
import { AuthModal as AuthModalBase } from '@affine/component/auth-components';
|
||||||
import { authAtom, type AuthAtomData } from '@affine/core/components/atoms';
|
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 { AuthService } from '@affine/core/modules/cloud';
|
||||||
import { apis, events } from '@affine/electron-api';
|
|
||||||
import { useI18n } from '@affine/i18n';
|
import { useI18n } from '@affine/i18n';
|
||||||
import { useLiveData, useService } from '@toeverything/infra';
|
import { useLiveData, useService } from '@toeverything/infra';
|
||||||
import { useAtom } from 'jotai/react';
|
import { useAtom } from 'jotai/react';
|
||||||
@@ -45,9 +43,7 @@ const config: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function AuthModal() {
|
export function AuthModal() {
|
||||||
const t = useI18n();
|
|
||||||
const [authAtomValue, setAuthAtom] = useAtom(authAtom);
|
const [authAtomValue, setAuthAtom] = useAtom(authAtom);
|
||||||
const authService = useService(AuthService);
|
|
||||||
const setOpen = useCallback(
|
const setOpen = useCallback(
|
||||||
(open: boolean) => {
|
(open: boolean) => {
|
||||||
setAuthAtom(prev => ({ ...prev, openModal: open }));
|
setAuthAtom(prev => ({ ...prev, openModal: open }));
|
||||||
@@ -55,45 +51,6 @@ export function AuthModal() {
|
|||||||
[setAuthAtom]
|
[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 (
|
return (
|
||||||
<AuthModalBase open={authAtomValue.openModal} setOpen={setOpen}>
|
<AuthModalBase open={authAtomValue.openModal} setOpen={setOpen}>
|
||||||
<AuthPanel />
|
<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 type { OAuthProviderType } from '@affine/graphql';
|
||||||
|
import { I18n } from '@affine/i18n';
|
||||||
import { track } from '@affine/track';
|
import { track } from '@affine/track';
|
||||||
import {
|
import {
|
||||||
ApplicationFocused,
|
ApplicationFocused,
|
||||||
@@ -56,6 +58,33 @@ export class AuthService extends Service {
|
|||||||
|
|
||||||
private onApplicationStart() {
|
private onApplicationStart() {
|
||||||
this.session.revalidate();
|
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() {
|
private onApplicationFocused() {
|
||||||
@@ -97,6 +126,8 @@ export class AuthService extends Service {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ email, token }),
|
body: JSON.stringify({ email, token }),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.session.revalidate();
|
||||||
track.$.$.auth.signedIn({ method: 'magic-link' });
|
track.$.$.auth.signedIn({ method: 'magic-link' });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
track.$.$.auth.signInFail({ method: 'magic-link' });
|
track.$.$.auth.signInFail({ method: 'magic-link' });
|
||||||
@@ -151,6 +182,8 @@ export class AuthService extends Service {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.session.revalidate();
|
||||||
|
|
||||||
track.$.$.auth.signedIn({ method: 'oauth', provider });
|
track.$.$.auth.signedIn({ method: 'oauth', provider });
|
||||||
return res.json();
|
return res.json();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -167,7 +200,7 @@ export class AuthService extends Service {
|
|||||||
}) {
|
}) {
|
||||||
track.$.$.auth.signIn({ method: 'password' });
|
track.$.$.auth.signIn({ method: 'password' });
|
||||||
try {
|
try {
|
||||||
const res = await this.fetchService.fetch('/api/auth/sign-in', {
|
await this.fetchService.fetch('/api/auth/sign-in', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(credential),
|
body: JSON.stringify(credential),
|
||||||
headers: {
|
headers: {
|
||||||
@@ -175,9 +208,6 @@ export class AuthService extends Service {
|
|||||||
...this.captchaHeaders(credential.verifyToken, credential.challenge),
|
...this.captchaHeaders(credential.verifyToken, credential.challenge),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error('Failed to sign in');
|
|
||||||
}
|
|
||||||
this.session.revalidate();
|
this.session.revalidate();
|
||||||
track.$.$.auth.signedIn({ method: 'password' });
|
track.$.$.auth.signedIn({ method: 'password' });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user