From c09704fc4fcf4921371e0362009a29cd6e59a488 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Thu, 17 Sep 2026 11:53:22 -0400 Subject: [PATCH] fix(auth): restore saved OAuth accounts when switching (#195) --- package.json | 2 +- .../__tests__/useAccountSwitcher.test.ts | 93 +++++++++++++++++++ src/lib/hooks/useAccountSwitcher.ts | 2 +- 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/lib/hooks/__tests__/useAccountSwitcher.test.ts diff --git a/package.json b/package.json index 00cc28070..49341309a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blacksky.community", - "version": "1.130.0", + "version": "1.130.1", "private": true, "packageManager": "pnpm@11.7.0", "engines": { diff --git a/src/lib/hooks/__tests__/useAccountSwitcher.test.ts b/src/lib/hooks/__tests__/useAccountSwitcher.test.ts new file mode 100644 index 000000000..379639cc9 --- /dev/null +++ b/src/lib/hooks/__tests__/useAccountSwitcher.test.ts @@ -0,0 +1,93 @@ +import {act, renderHook} from '@testing-library/react-native' + +import {type SessionAccount} from '#/state/session' + +const mockResumeSession = jest.fn() +const mockRequestSwitchToAccount = jest.fn() +const mockMetric = jest.fn() + +jest.mock('#/state/session', () => ({ + useSessionApi: () => ({resumeSession: mockResumeSession}), +})) +jest.mock('#/state/shell/logged-out', () => ({ + useLoggedOutViewControls: () => ({ + requestSwitchToAccount: mockRequestSwitchToAccount, + }), +})) +jest.mock('#/analytics', () => ({ + useAnalytics: () => ({metric: mockMetric}), +})) +jest.mock('@lingui/react', () => ({ + useLingui: () => ({_: (message: {message: string}) => message.message}), +})) +jest.mock('#/components/Toast', () => ({show: jest.fn()})) +jest.mock('#/logger', () => ({logger: {error: jest.fn()}})) +jest.mock('#/env', () => ({IS_WEB: false})) + +import {useAccountSwitcher} from '../useAccountSwitcher' + +const account: SessionAccount = { + did: 'did:plc:test', + handle: 'test.example', + service: 'https://pds.example', +} + +describe('useAccountSwitcher', () => { + beforeEach(() => { + jest.resetAllMocks() + mockResumeSession.mockResolvedValue(undefined) + }) + + it.each([ + ['OAuth', {...account, isOauthSession: true}], + ['legacy', {...account, accessJwt: 'saved-access-token'}], + ])( + 'restores a saved %s account without asking for sign-in', + async (_, saved) => { + const {result} = renderHook(() => useAccountSwitcher()) + + await act(async () => { + await result.current.onPressSwitchAccount(saved, 'SwitchAccount') + }) + + expect(mockResumeSession).toHaveBeenCalledWith(saved, true) + expect(mockRequestSwitchToAccount).not.toHaveBeenCalled() + expect(mockMetric).toHaveBeenCalledWith('account:loggedIn', { + logContext: 'SwitchAccount', + withPassword: false, + }) + expect(result.current.pendingDid).toBeNull() + }, + ) + + it('asks for sign-in when a legacy account has no saved credentials', async () => { + const {result} = renderHook(() => useAccountSwitcher()) + + await act(async () => { + await result.current.onPressSwitchAccount(account, 'SwitchAccount') + }) + + expect(mockResumeSession).not.toHaveBeenCalled() + expect(mockRequestSwitchToAccount).toHaveBeenCalledWith({ + requestedAccount: account.did, + }) + expect(mockMetric).not.toHaveBeenCalled() + }) + + it('falls back to sign-in when OAuth restoration fails', async () => { + mockResumeSession.mockRejectedValue(new Error('Session unavailable')) + const saved = {...account, isOauthSession: true} + const {result} = renderHook(() => useAccountSwitcher()) + + await act(async () => { + await result.current.onPressSwitchAccount(saved, 'SwitchAccount') + }) + + expect(mockResumeSession).toHaveBeenCalledWith(saved, true) + expect(mockRequestSwitchToAccount).toHaveBeenCalledWith({ + requestedAccount: account.did, + }) + expect(mockMetric).not.toHaveBeenCalled() + expect(result.current.pendingDid).toBeNull() + }) +}) diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index 106e92d37..5f80d6d55 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -28,7 +28,7 @@ export function useAccountSwitcher() { } try { setPendingDid(account.did) - if (account.accessJwt) { + if (account.isOauthSession || account.accessJwt) { if (IS_WEB) { // We're switching accounts, which remounts the entire app. // On mobile, this gets us Home, but on the web we also need reset the URL. -- knotmirror 0.0.1