refactor: remove hacky email login (#4075)

Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
Peng Xiao
2023-09-01 01:49:22 +08:00
committed by GitHub
parent f99a7a5ecd
commit a2e4ef904b
13 changed files with 78 additions and 153 deletions

View File

@@ -1,13 +0,0 @@
import { isDesktop } from '@affine/env/constant';
export function buildCallbackUrl(callbackUrl: string) {
const params: string[][] = [];
if (isDesktop && window.appInfo.schema) {
params.push(['schema', window.appInfo.schema]);
}
const query =
params.length > 0
? '?' + params.map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')
: '';
return callbackUrl + query;
}

View File

@@ -83,8 +83,8 @@ export const SignIn: FC<AuthPanelProps> = ({
marginTop: 30,
}}
icon={<GoogleDuotoneIcon />}
onClick={useCallback(async () => {
await signInWithGoogle();
onClick={useCallback(() => {
signInWithGoogle();
}, [signInWithGoogle])}
>
{t['Continue with Google']()}

View File

@@ -1,12 +1,10 @@
import { pushNotificationAtom } from '@affine/component/notification-center';
import type { Notification } from '@affine/component/notification-center/index.jotai';
import { isDesktop } from '@affine/env/constant';
import { atom, useAtom, useSetAtom } from 'jotai';
import { type SignInResponse } from 'next-auth/react';
import { useCallback } from 'react';
import { signInCloud } from '../../../utils/cloud-utils';
import { buildCallbackUrl } from './callback-url';
const COUNT_DOWN_TIME = 60;
const INTERNAL_BETA_URL = `https://community.affine.pro/c/insider-general/`;
@@ -77,7 +75,7 @@ export const useAuth = ({ onNoAccess }: { onNoAccess: () => void }) => {
const res = await signInCloud('email', {
email: email,
callbackUrl: buildCallbackUrl('signIn'),
callbackUrl: '/auth/signIn',
redirect: false,
}).catch(console.error);
@@ -100,7 +98,7 @@ export const useAuth = ({ onNoAccess }: { onNoAccess: () => void }) => {
const res = await signInCloud('email', {
email: email,
callbackUrl: buildCallbackUrl('signUp'),
callbackUrl: '/auth/signUp',
redirect: false,
}).catch(console.error);
@@ -114,16 +112,7 @@ export const useAuth = ({ onNoAccess }: { onNoAccess: () => void }) => {
);
const signInWithGoogle = useCallback(() => {
if (isDesktop) {
open(
`/desktop-signin?provider=google&callback_url=${buildCallbackUrl(
'/open-app/oauth-jwt'
)}`,
'_target'
);
} else {
signInCloud('google').catch(console.error);
}
signInCloud('google').catch(console.error);
}, []);
return {

View File

@@ -76,8 +76,10 @@ const OpenAppImpl = ({ urlToOpen, channel }: OpenAppProps) => {
if (!urlToOpen || lastOpened === urlToOpen || !autoOpen) {
return;
}
lastOpened = urlToOpen;
open(urlToOpen, '_blank');
setTimeout(() => {
lastOpened = urlToOpen;
open(urlToOpen, '_blank');
}, 1000);
}, [urlToOpen, autoOpen]);
if (!urlToOpen) {

View File

@@ -164,7 +164,7 @@ export const DesktopLoginModal = (): ReactElement => {
useEffect(() => {
return window.events?.ui.onFinishLogin(({ success, email }) => {
if (email !== signingEmail) {
if (email && email !== signingEmail) {
return;
}
setSigningEmail(undefined);

View File

@@ -1,15 +1,36 @@
import { isDesktop } from '@affine/env/constant';
import { refreshRootMetadataAtom } from '@affine/workspace/atom';
import { getCurrentStore } from '@toeverything/infra/atom';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { signIn, signOut } from 'next-auth/react';
import { startTransition } from 'react';
export const signInCloud: typeof signIn = async (...args) => {
return signIn(...args).then(result => {
// do not refresh root metadata,
// because the session won't change in this callback
return result;
});
export const signInCloud: typeof signIn = async (provider, ...rest) => {
if (isDesktop) {
if (provider === 'google') {
open(
`/desktop-signin?provider=google&callback_url=${buildCallbackUrl(
'/open-app/oauth-jwt'
)}`,
'_target'
);
return;
} else if (provider === 'email') {
const [options, ...tail] = rest;
return signIn(
provider,
{
...options,
callbackUrl: buildCallbackUrl('/open-app/oauth-jwt'),
},
...tail
);
} else {
throw new Error('Unsupported provider');
}
} else {
return signIn(provider, ...rest);
}
};
export const signOutCloud: typeof signOut = async (...args) => {
@@ -22,3 +43,15 @@ export const signOutCloud: typeof signOut = async (...args) => {
return result;
});
};
export function buildCallbackUrl(callbackUrl: string) {
const params: string[][] = [];
if (isDesktop && window.appInfo.schema) {
params.push(['schema', window.appInfo.schema]);
}
const query =
params.length > 0
? '?' + params.map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')
: '';
return callbackUrl + query;
}