fix: signout account when logging through oauth signin via desktop (#4321)

This commit is contained in:
Peng Xiao 2023-09-13 10:10:03 +08:00 committed by GitHub
parent 7d6c150ecd
commit 9ec2b9cf51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 15 deletions

View File

@ -1,7 +1,8 @@
import type { LoaderFunction } from 'react-router-dom'; import { getSession } from 'next-auth/react';
import { type LoaderFunction } from 'react-router-dom';
import { z } from 'zod'; import { z } from 'zod';
import { signInCloud } from '../utils/cloud-utils'; import { signInCloud, signOutCloud } from '../utils/cloud-utils';
const supportedProvider = z.enum(['google']); const supportedProvider = z.enum(['google']);
@ -13,6 +14,16 @@ export const loader: LoaderFunction = async ({ request }) => {
if (!callback_url) { if (!callback_url) {
return null; return null;
} }
const session = await getSession();
if (session) {
// already signed in, need to sign out first
await signOutCloud({
callbackUrl: request.url, // retry
});
}
const maybeProvider = supportedProvider.safeParse(provider); const maybeProvider = supportedProvider.safeParse(provider);
if (maybeProvider.success) { if (maybeProvider.success) {
const provider = maybeProvider.data; const provider = maybeProvider.data;

View File

@ -57,7 +57,7 @@ interface OpenAppProps {
} }
interface LoaderData { interface LoaderData {
action: 'url' | 'oauth-jwt'; action: 'url' | 'signin-redirect';
currentUser?: GetCurrentUserQuery['currentUser']; currentUser?: GetCurrentUserQuery['currentUser'];
} }
@ -86,12 +86,7 @@ const OpenAppImpl = ({ urlToOpen, channel }: OpenAppProps) => {
return ( return (
<div className={styles.root}> <div className={styles.root}>
<div className={styles.topNav}> <div className={styles.topNav}>
<a <a href="/" rel="noreferrer" className={styles.affineLogo}>
href="https://affine.pro"
target="_blank"
rel="noreferrer"
className={styles.affineLogo}
>
<Logo1Icon width={24} height={24} /> <Logo1Icon width={24} height={24} />
</a> </a>
@ -182,7 +177,7 @@ const OpenOAuthJwt = () => {
return null; return null;
} }
const urlToOpen = `${schema}://oauth-jwt?token=${currentUser.token.sessionToken}`; const urlToOpen = `${schema}://signin-redirect?token=${currentUser.token.sessionToken}`;
return <OpenAppImpl urlToOpen={urlToOpen} channel={channel} />; return <OpenAppImpl urlToOpen={urlToOpen} channel={channel} />;
}; };
@ -192,7 +187,7 @@ export const Component = () => {
if (action === 'url') { if (action === 'url') {
return <OpenUrl />; return <OpenUrl />;
} else if (action === 'oauth-jwt') { } else if (action === 'signin-redirect') {
return <OpenOAuthJwt />; return <OpenOAuthJwt />;
} }
return null; return null;

View File

@ -54,7 +54,7 @@ export const signInCloud: typeof signIn = async (provider, ...rest) => {
`${ `${
runtimeConfig.serverUrlPrefix runtimeConfig.serverUrlPrefix
}/desktop-signin?provider=google&callback_url=${buildCallbackUrl( }/desktop-signin?provider=google&callback_url=${buildCallbackUrl(
'/open-app/oauth-jwt' '/open-app/signin-redirect'
)}`, )}`,
'_target' '_target'
); );
@ -63,7 +63,9 @@ export const signInCloud: typeof signIn = async (provider, ...rest) => {
const [options, ...tail] = rest; const [options, ...tail] = rest;
const callbackUrl = const callbackUrl =
runtimeConfig.serverUrlPrefix + runtimeConfig.serverUrlPrefix +
(provider === 'email' ? '/open-app/oauth-jwt' : location.pathname); (provider === 'email'
? '/open-app/signin-redirect'
: location.pathname);
return signIn( return signIn(
provider, provider,
{ {
@ -85,8 +87,8 @@ export const signInCloud: typeof signIn = async (provider, ...rest) => {
export const signOutCloud: typeof signOut = async options => { export const signOutCloud: typeof signOut = async options => {
const traceParams = genTraceParams(); const traceParams = genTraceParams();
return signOut({ return signOut({
...options,
callbackUrl: '/', callbackUrl: '/',
...options,
}) })
.then(result => { .then(result => {
if (result) { if (result) {

View File

@ -59,7 +59,7 @@ async function handleAffineUrl(url: string) {
logger.info('handle affine schema action', urlObj.hostname); logger.info('handle affine schema action', urlObj.hostname);
// handle more actions here // handle more actions here
// hostname is the action name // hostname is the action name
if (urlObj.hostname === 'oauth-jwt') { if (urlObj.hostname === 'signin-redirect') {
await handleOauthJwt(url); await handleOauthJwt(url);
} }
} }