mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-26 14:02:57 +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 { 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;
|
||||||
}>
|
}>
|
||||||
) => {
|
) => {
|
||||||
|
// 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);
|
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;
|
||||||
};
|
});
|
||||||
|
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 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])}
|
||||||
|
Loading…
Reference in New Issue
Block a user