From e9a11114d384d5ddf8b193da474c4a1b6be5c129 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Fri, 24 Nov 2023 17:41:42 -0600 Subject: [PATCH] [PWI] Clarify different ways of clearing current account/logout (#1991) * Clarify different ways of clearing current account/logout * Reorder log --- src/state/session/index.tsx | 46 +++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 1e7fa22976..946c742ad7 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -42,7 +42,20 @@ export type ApiContext = { identifier: string password: string }) => Promise + /** + * A full logout. Clears the `currentAccount` from session, AND removes + * access tokens from all accounts, so that returning as any user will + * require a full login. + */ logout: () => Promise + /** + * A partial logout. Clears the `currentAccount` from session, but DOES NOT + * clear access tokens from accounts, allowing the user to return to their + * other accounts without logging in. + * + * Used when adding a new account, deleting an account. + */ + clearCurrentAccount: () => void initSession: (account: SessionAccount) => Promise resumeSession: (account?: SessionAccount) => Promise removeAccount: (account: SessionAccount) => void @@ -52,7 +65,6 @@ export type ApiContext = { Pick >, ) => void - clearCurrentAccount: () => void } const StateContext = React.createContext({ @@ -256,13 +268,26 @@ export function Provider({children}: React.PropsWithChildren<{}>) { [upsertAccount, queryClient], ) + const clearCurrentAccount = React.useCallback(() => { + logger.debug( + `session: clear current account`, + {}, + logger.DebugContext.session, + ) + __globalAgent = PUBLIC_BSKY_AGENT + queryClient.clear() + setStateAndPersist(s => ({ + ...s, + currentAccount: undefined, + })) + }, [setStateAndPersist, queryClient]) + const logout = React.useCallback(async () => { + clearCurrentAccount() logger.debug(`session: logout`, {}, logger.DebugContext.session) setStateAndPersist(s => { return { ...s, - agent: PUBLIC_BSKY_AGENT, - currentAccount: undefined, accounts: s.accounts.map(a => ({ ...a, refreshJwt: undefined, @@ -270,7 +295,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { })), } }) - }, [setStateAndPersist]) + }, [clearCurrentAccount, setStateAndPersist]) const initSession = React.useCallback( async account => { @@ -404,19 +429,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) { [setState, initSession], ) - /** - * Clears the `currentAccount` from session. Typically used to drop the user - * back to the sign-in page. - */ - const clearCurrentAccount = React.useCallback(() => { - __globalAgent = PUBLIC_BSKY_AGENT - queryClient.clear() - setStateAndPersist(s => ({ - ...s, - currentAccount: undefined, - })) - }, [setStateAndPersist, queryClient]) - React.useEffect(() => { if (isDirty.current) { isDirty.current = false