From 1ed2c757dc150788fa6036fff20d3ea4526cd29b Mon Sep 17 00:00:00 2001 From: hailey Date: Thu, 27 Aug 2026 23:50:03 +0000 Subject: [PATCH] Handle empty current logout state --- src/state/session/__tests__/session-test.ts | 26 ++++++++++++++++++++- src/state/session/reducer.ts | 8 +++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index 5b47271e8b..7a4ea0d4ff 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -1902,13 +1902,37 @@ describe('session', () => { const rebased = rebasePersistedSession( snapshot([fresh], 'alice-did'), snapshot([loggedOut]), - {type: 'logged-out-current-account'}, + {type: 'logged-out-current-account', accountDid: 'alice-did'}, ) expect(rebased.accounts).toEqual([loggedOut]) expect(rebased.currentAccount).toBeUndefined() }) + it('keeps latest tokens when no current account is available to log out', () => { + const stale = makeAccount('https://alice.com', { + active: true, + did: 'alice-did', + handle: 'alice.test', + accessJwt: 'alice-access-jwt-1', + refreshJwt: 'alice-refresh-jwt-1', + }) + const fresh = { + ...stale, + accessJwt: 'alice-access-jwt-2', + refreshJwt: 'alice-refresh-jwt-2', + } + + const rebased = rebasePersistedSession( + snapshot([fresh], 'alice-did'), + snapshot([stale]), + {type: 'logged-out-current-account'}, + ) + + expect(rebased.accounts).toEqual([fresh]) + expect(rebased.currentAccount).toBeUndefined() + }) + it('clears only the explicitly logged-out account', () => { const alice = makeAccount('https://alice.com', { active: true, diff --git a/src/state/session/reducer.ts b/src/state/session/reducer.ts index 32fc7bf834..03bd294fb2 100644 --- a/src/state/session/reducer.ts +++ b/src/state/session/reducer.ts @@ -164,9 +164,7 @@ function getTokenWriteMode( ): 'replace' | 'clear' | 'keep-latest' { switch (action.type) { case 'logged-out-current-account': - return action.accountDid === undefined || action.accountDid === did - ? 'clear' - : 'keep-latest' + return action.accountDid === did ? 'clear' : 'keep-latest' case 'logged-out-every-account': return 'clear' case 'received-session-event': @@ -175,8 +173,8 @@ function getTokenWriteMode( return latestAccount.refreshJwt ? 'replace' : 'keep-latest' } if (action.sessionEvent === 'expired') { - return latestAccount.refreshJwt && - latestAccount.refreshJwt !== action.expiredRefreshJwt + if (!action.expiredRefreshJwt) return 'clear' + return latestAccount.refreshJwt !== action.expiredRefreshJwt ? 'keep-latest' : 'clear' }