mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-30 00:29:46 +08:00
feat: refactor user & login function
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
PageMeta,
|
||||
} from './interface';
|
||||
import { createDefaultWorkspace } from './utils';
|
||||
import { WorkspaceInfo } from '@affine/datacenter';
|
||||
import { WorkspaceInfo, User } from '@affine/datacenter';
|
||||
|
||||
type AppStateContextProps = PropsWithChildren<Record<string, unknown>>;
|
||||
|
||||
@@ -127,6 +127,26 @@ export const AppStateProvider = ({
|
||||
});
|
||||
};
|
||||
|
||||
const login = async () => {
|
||||
const { dataCenter } = appState;
|
||||
await dataCenter.login();
|
||||
const user = (await dataCenter.getUserInfo()) as User;
|
||||
setAppState({
|
||||
...appState,
|
||||
user,
|
||||
});
|
||||
return user;
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
const { dataCenter } = appState;
|
||||
await dataCenter.logout();
|
||||
setAppState({
|
||||
...appState,
|
||||
user: null,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<AppState.Provider
|
||||
value={{
|
||||
@@ -134,6 +154,8 @@ export const AppStateProvider = ({
|
||||
setEditor,
|
||||
loadPage: loadPage.current,
|
||||
loadWorkspace: loadWorkspace.current,
|
||||
login,
|
||||
logout,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
@@ -33,6 +33,9 @@ export type AppStateFunction = {
|
||||
|
||||
loadWorkspace: (workspaceId: string) => Promise<StoreWorkspace | null>;
|
||||
loadPage: (pageId: string) => void;
|
||||
|
||||
login: () => Promise<User>;
|
||||
logout: () => Promise<void>;
|
||||
};
|
||||
|
||||
export type AppStateContext = AppStateValue & AppStateFunction;
|
||||
|
||||
Reference in New Issue
Block a user