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
+39 -6
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;
}