fix: patch refresh token logic (#1665)

This commit is contained in:
Himself65 2023-03-23 00:12:20 -05:00 committed by GitHub
parent 059d9e5de2
commit 2551785451
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 21 deletions

View File

@ -1,8 +1,11 @@
import { toast } from '@affine/component'; import { toast } from '@affine/component';
import type { MessageCode } from '@affine/datacenter'; import { MessageCode } from '@affine/datacenter';
import { messages } from '@affine/datacenter'; import { messages } from '@affine/datacenter';
import type React from 'react'; import type React from 'react';
import { useEffect } from 'react'; import { memo, useEffect, useState } from 'react';
import { useOnGoogleLogout } from '../../../hooks/use-on-google-logout';
import { apis } from '../../../shared/apis';
declare global { declare global {
interface DocumentEventMap { interface DocumentEventMap {
@ -12,20 +15,42 @@ declare global {
} }
} }
export const MessageCenter: React.FC = () => { export const MessageCenter: React.FC = memo(function MessageCenter() {
const [popup, setPopup] = useState(false);
const onLogout = useOnGoogleLogout();
useEffect(() => { useEffect(() => {
const listener = ( const listener = (
event: CustomEvent<{ event: CustomEvent<{
code: MessageCode; code: MessageCode;
}> }>
) => { ) => {
toast(messages[event.detail.code].message); // fixme: need refactor
// - login and refresh refresh logic should be refactored
// - error message should be refactored
if (
!popup &&
(event.detail.code === MessageCode.refreshTokenError ||
event.detail.code === MessageCode.loginError)
) {
setPopup(true);
apis
.signInWithGoogle()
.then(() => {
setPopup(false);
})
.catch(() => {
setPopup(false);
onLogout();
});
} else {
toast(messages[event.detail.code].message);
}
}; };
document.addEventListener('affine-error', listener); document.addEventListener('affine-error', listener);
return () => { return () => {
document.removeEventListener('affine-error', listener); document.removeEventListener('affine-error', listener);
}; };
}, []); }, [onLogout, popup]);
return null; return null;
}; });

View File

@ -0,0 +1,23 @@
import { useSetAtom } from 'jotai';
import { useRouter } from 'next/router';
import { useCallback } from 'react';
import { jotaiWorkspacesAtom } from '../atoms';
import { WorkspacePlugins } from '../plugins';
import { RemWorkspaceFlavour } from '../shared';
import { apis } from '../shared/apis';
export function useOnGoogleLogout() {
const set = useSetAtom(jotaiWorkspacesAtom);
const router = useRouter();
return useCallback(() => {
apis.auth.clear();
set(workspaces =>
workspaces.filter(
workspace => workspace.flavour !== RemWorkspaceFlavour.AFFINE
)
);
WorkspacePlugins[RemWorkspaceFlavour.AFFINE].cleanup?.();
router.reload();
}, [router, set]);
}

View File

@ -1,4 +1,4 @@
import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import { useAtom, useAtomValue } from 'jotai';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import type React from 'react'; import type React from 'react';
@ -6,16 +6,15 @@ import { useCallback } from 'react';
import { import {
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
jotaiWorkspacesAtom,
openCreateWorkspaceModalAtom, openCreateWorkspaceModalAtom,
openWorkspacesModalAtom, openWorkspacesModalAtom,
} from '../atoms'; } from '../atoms';
import { useCurrentUser } from '../hooks/current/use-current-user'; import { useCurrentUser } from '../hooks/current/use-current-user';
import { useCurrentWorkspace } from '../hooks/current/use-current-workspace'; import { useCurrentWorkspace } from '../hooks/current/use-current-workspace';
import { useOnGoogleLogout } from '../hooks/use-on-google-logout';
import { useRouterHelper } from '../hooks/use-router-helper'; import { useRouterHelper } from '../hooks/use-router-helper';
import { useWorkspaces, useWorkspacesHelper } from '../hooks/use-workspaces'; import { useWorkspaces, useWorkspacesHelper } from '../hooks/use-workspaces';
import { WorkspacePlugins } from '../plugins'; import { WorkspaceSubPath } from '../shared';
import { RemWorkspaceFlavour, WorkspaceSubPath } from '../shared';
import { apis } from '../shared/apis'; import { apis } from '../shared/apis';
const WorkspaceListModal = dynamic( const WorkspaceListModal = dynamic(
@ -43,7 +42,6 @@ export function Modals() {
const currentWorkspaceId = useAtomValue(currentWorkspaceIdAtom); const currentWorkspaceId = useAtomValue(currentWorkspaceIdAtom);
const [, setCurrentWorkspace] = useCurrentWorkspace(); const [, setCurrentWorkspace] = useCurrentWorkspace();
const { createLocalWorkspace } = useWorkspacesHelper(); const { createLocalWorkspace } = useWorkspacesHelper();
const set = useSetAtom(jotaiWorkspacesAtom);
return ( return (
<> <>
@ -76,16 +74,7 @@ export function Modals() {
router.reload(); router.reload();
}); });
}, [router])} }, [router])}
onClickLogout={useCallback(() => { onClickLogout={useOnGoogleLogout()}
apis.auth.clear();
set(workspaces =>
workspaces.filter(
workspace => workspace.flavour !== RemWorkspaceFlavour.AFFINE
)
);
WorkspacePlugins[RemWorkspaceFlavour.AFFINE].cleanup?.();
router.reload();
}, [router, set])}
onCreateWorkspace={useCallback(() => { onCreateWorkspace={useCallback(() => {
setOpenCreateWorkspaceModal(true); setOpenCreateWorkspaceModal(true);
}, [setOpenCreateWorkspaceModal])} }, [setOpenCreateWorkspaceModal])}