about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2023-11-24 17:41:42 -0600
committerGitHub <noreply@github.com>2023-11-24 17:41:42 -0600
commite9a11114d384d5ddf8b193da474c4a1b6be5c129 (patch)
treee1546c02d80c32bf4e348794fa23c436c936ee04 /src
parent20b699a008d31763bf4b93f9e6d0582eb516579f (diff)
downloadvoidsky-e9a11114d384d5ddf8b193da474c4a1b6be5c129.tar.zst
[PWI] Clarify different ways of clearing current account/logout (#1991)
* Clarify different ways of clearing current account/logout

* Reorder log
Diffstat (limited to 'src')
-rw-r--r--src/state/session/index.tsx46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx
index 1e7fa2297..946c742ad 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<void>
+  /**
+   * 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<void>
+  /**
+   * 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<void>
   resumeSession: (account?: SessionAccount) => Promise<void>
   removeAccount: (account: SessionAccount) => void
@@ -52,7 +65,6 @@ export type ApiContext = {
       Pick<SessionAccount, 'handle' | 'email' | 'emailConfirmed'>
     >,
   ) => void
-  clearCurrentAccount: () => void
 }
 
 const StateContext = React.createContext<StateContext>({
@@ -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<ApiContext['logout']>(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<ApiContext['initSession']>(
     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