Skip to content

Commit

Permalink
fix(core): fix web login
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Sep 19, 2024
1 parent fc9e5fb commit b4e2284
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 48 deletions.
43 changes: 0 additions & 43 deletions packages/frontend/core/src/components/affine/auth/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -45,55 +43,14 @@ 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 }));
},
[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 />
Expand Down
40 changes: 35 additions & 5 deletions packages/frontend/core/src/modules/cloud/services/auth.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -151,6 +182,8 @@ export class AuthService extends Service {
},
});

this.session.revalidate();

track.$.$.auth.signedIn({ method: 'oauth', provider });
return res.json();
} catch (e) {
Expand All @@ -167,17 +200,14 @@ 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: {
'content-type': 'application/json',
...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) {
Expand Down

0 comments on commit b4e2284

Please sign in to comment.