mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-22 09:13:18 +03:00
fix: patch refresh token logic (#1665)
This commit is contained in:
parent
059d9e5de2
commit
2551785451
@ -1,8 +1,11 @@
|
||||
import { toast } from '@affine/component';
|
||||
import type { MessageCode } from '@affine/datacenter';
|
||||
import { MessageCode } from '@affine/datacenter';
|
||||
import { messages } from '@affine/datacenter';
|
||||
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 {
|
||||
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(() => {
|
||||
const listener = (
|
||||
event: CustomEvent<{
|
||||
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);
|
||||
return () => {
|
||||
document.removeEventListener('affine-error', listener);
|
||||
};
|
||||
}, []);
|
||||
}, [onLogout, popup]);
|
||||
return null;
|
||||
};
|
||||
});
|
||||
|
23
apps/web/src/hooks/use-on-google-logout.ts
Normal file
23
apps/web/src/hooks/use-on-google-logout.ts
Normal 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]);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import dynamic from 'next/dynamic';
|
||||
import { useRouter } from 'next/router';
|
||||
import type React from 'react';
|
||||
@ -6,16 +6,15 @@ import { useCallback } from 'react';
|
||||
|
||||
import {
|
||||
currentWorkspaceIdAtom,
|
||||
jotaiWorkspacesAtom,
|
||||
openCreateWorkspaceModalAtom,
|
||||
openWorkspacesModalAtom,
|
||||
} from '../atoms';
|
||||
import { useCurrentUser } from '../hooks/current/use-current-user';
|
||||
import { useCurrentWorkspace } from '../hooks/current/use-current-workspace';
|
||||
import { useOnGoogleLogout } from '../hooks/use-on-google-logout';
|
||||
import { useRouterHelper } from '../hooks/use-router-helper';
|
||||
import { useWorkspaces, useWorkspacesHelper } from '../hooks/use-workspaces';
|
||||
import { WorkspacePlugins } from '../plugins';
|
||||
import { RemWorkspaceFlavour, WorkspaceSubPath } from '../shared';
|
||||
import { WorkspaceSubPath } from '../shared';
|
||||
import { apis } from '../shared/apis';
|
||||
|
||||
const WorkspaceListModal = dynamic(
|
||||
@ -43,7 +42,6 @@ export function Modals() {
|
||||
const currentWorkspaceId = useAtomValue(currentWorkspaceIdAtom);
|
||||
const [, setCurrentWorkspace] = useCurrentWorkspace();
|
||||
const { createLocalWorkspace } = useWorkspacesHelper();
|
||||
const set = useSetAtom(jotaiWorkspacesAtom);
|
||||
|
||||
return (
|
||||
<>
|
||||
@ -76,16 +74,7 @@ export function Modals() {
|
||||
router.reload();
|
||||
});
|
||||
}, [router])}
|
||||
onClickLogout={useCallback(() => {
|
||||
apis.auth.clear();
|
||||
set(workspaces =>
|
||||
workspaces.filter(
|
||||
workspace => workspace.flavour !== RemWorkspaceFlavour.AFFINE
|
||||
)
|
||||
);
|
||||
WorkspacePlugins[RemWorkspaceFlavour.AFFINE].cleanup?.();
|
||||
router.reload();
|
||||
}, [router, set])}
|
||||
onClickLogout={useOnGoogleLogout()}
|
||||
onCreateWorkspace={useCallback(() => {
|
||||
setOpenCreateWorkspaceModal(true);
|
||||
}, [setOpenCreateWorkspaceModal])}
|
||||
|
Loading…
Reference in New Issue
Block a user