Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/auth-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ export function AuthProvider({ children }) {
}
}
} catch {
// corrupt blob -> treat as signed out
// Corrupt blob (JSON.parse failed) -> purge it so we don't re-read and
// re-fail on every cold start, then treat as signed out. Mirrors the
// expired/issuer-mismatch branch above.
try {
await SecureStore.deleteItemAsync(STORAGE_KEY);
} catch {
// best-effort cleanup; nothing more we can do here.
}
} finally {
if (!cancelled) setLoading(false);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/auth-context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ describe('AuthProvider lifecycle', () => {
expect(captured.user?.email).toBe('restored@example.com');
});

test('purges a corrupt SecureStore blob on mount (parse failure)', async () => {
// Not valid JSON -> JSON.parse throws inside the restore effect.
mockMemStore.set(STORAGE_KEY, '{not-json');
let captured;
render(
<AuthProvider>
<Probe onUser={(c) => (captured = c)} />
</AuthProvider>,
);
await waitFor(() => expect(captured.loading).toBe(false));
expect(captured.user).toBeNull();
// The bad blob must be deleted so the next cold start doesn't re-fail.
expect(mockSecureStore.deleteItemAsync).toHaveBeenCalledWith(STORAGE_KEY);
expect(mockMemStore.has(STORAGE_KEY)).toBe(false);
});

test('signOut clears user + SecureStore', async () => {
mockMemStore.set(
STORAGE_KEY,
Expand Down