-
Notifications
You must be signed in to change notification settings - Fork 473
fix(expo): keep hasCredentials false when the biometric prompt is cancelled #9869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wobsoriano
merged 2 commits into
main
from
rob/mobile-637-expo-cancelling-biometric-prompt-during-setcredentials
Sep 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/expo': patch | ||
| --- | ||
|
|
||
| Fix `useLocalCredentials()` reporting `hasCredentials` as `true` after the biometric prompt is cancelled during `setCredentials()`. A cancelled prompt now leaves the stored credentials unchanged, so `authenticate()` no longer fails with a missing password. |
117 changes: 117 additions & 0 deletions
117
...ages/expo/src/local-credentials/useLocalCredentials/__tests__/useLocalCredentials.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { useLocalCredentials } from '../useLocalCredentials'; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| publishableKey: 'pk_test_Zm9vLmNsZXJrLmFjY291bnRzLmRldiQ', | ||
| store: new Map<string, string>(), | ||
| rejectProtectedWrites: false, | ||
| signIn: { create: vi.fn() }, | ||
| })); | ||
|
|
||
| vi.mock('@clerk/react', () => ({ | ||
| useClerk: () => ({ publishableKey: mocks.publishableKey }), | ||
| useUser: () => ({ user: null }), | ||
| })); | ||
|
|
||
| vi.mock('@clerk/react/legacy', () => ({ | ||
| useSignIn: () => ({ isLoaded: true, signIn: mocks.signIn }), | ||
| })); | ||
|
|
||
| vi.mock('react-native', () => ({ | ||
| Platform: { OS: 'ios' }, | ||
| })); | ||
|
|
||
| vi.mock('../../../utils/native-module', () => ({ | ||
| ClerkExpoModule: {}, | ||
| })); | ||
|
|
||
| vi.mock('expo-local-authentication', () => ({ | ||
| AuthenticationType: { FINGERPRINT: 1, FACIAL_RECOGNITION: 2, IRIS: 3 }, | ||
| isEnrolledAsync: () => Promise.resolve(true), | ||
| supportedAuthenticationTypesAsync: () => Promise.resolve([]), | ||
| })); | ||
|
|
||
| vi.mock('expo-secure-store', () => ({ | ||
| WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: 0, | ||
| getItem: (key: string) => mocks.store.get(key) ?? null, | ||
| getItemAsync: (key: string) => Promise.resolve(mocks.store.get(key) ?? null), | ||
| deleteItemAsync: (key: string) => { | ||
| mocks.store.delete(key); | ||
| return Promise.resolve(); | ||
| }, | ||
| setItemAsync: (key: string, value: string, options?: { requireAuthentication?: boolean }) => { | ||
| if (options?.requireAuthentication && mocks.rejectProtectedWrites) { | ||
| return Promise.reject(new Error('User canceled the authentication')); | ||
| } | ||
| mocks.store.set(key, value); | ||
| return Promise.resolve(); | ||
| }, | ||
| })); | ||
|
|
||
| const identifierKey = `__clerk_local_auth_${mocks.publishableKey}_identifier`; | ||
| const passwordKey = `__clerk_local_auth_${mocks.publishableKey}_password`; | ||
|
|
||
| beforeEach(() => { | ||
| mocks.store.clear(); | ||
| mocks.rejectProtectedWrites = false; | ||
| }); | ||
|
|
||
| describe('useLocalCredentials', () => { | ||
| test('reports credentials once both writes succeed', async () => { | ||
| const { result } = renderHook(() => useLocalCredentials()); | ||
|
|
||
| await act(() => result.current.setCredentials({ identifier: 'user@example.com', password: 'hunter2' })); | ||
|
|
||
| expect(result.current.hasCredentials).toBe(true); | ||
| expect(mocks.store.get(identifierKey)).toBe('user@example.com'); | ||
| expect(mocks.store.get(passwordKey)).toBe('hunter2'); | ||
| }); | ||
|
|
||
| test('does not report credentials when the biometric prompt is cancelled', async () => { | ||
| mocks.rejectProtectedWrites = true; | ||
| const { result } = renderHook(() => useLocalCredentials()); | ||
|
|
||
| await act(async () => { | ||
| await expect( | ||
| result.current.setCredentials({ identifier: 'user@example.com', password: 'hunter2' }), | ||
| ).rejects.toThrow('User canceled the authentication'); | ||
| }); | ||
|
|
||
| expect(result.current.hasCredentials).toBe(false); | ||
| expect(mocks.store.has(identifierKey)).toBe(false); | ||
| expect(mocks.store.has(passwordKey)).toBe(false); | ||
|
|
||
| const remounted = renderHook(() => useLocalCredentials()); | ||
| expect(remounted.result.current.hasCredentials).toBe(false); | ||
| }); | ||
|
|
||
| test('keeps the existing credentials when a password update is cancelled', async () => { | ||
| mocks.store.set(identifierKey, 'user@example.com'); | ||
| mocks.store.set(passwordKey, 'hunter2'); | ||
| mocks.rejectProtectedWrites = true; | ||
| const { result } = renderHook(() => useLocalCredentials()); | ||
|
|
||
| await act(async () => { | ||
| await expect(result.current.setCredentials({ password: 'new-password' })).rejects.toThrow(); | ||
| }); | ||
|
|
||
| expect(result.current.hasCredentials).toBe(true); | ||
| expect(mocks.store.get(identifierKey)).toBe('user@example.com'); | ||
| expect(mocks.store.get(passwordKey)).toBe('hunter2'); | ||
| }); | ||
|
|
||
| test('rejects a password update when no identifier is stored', async () => { | ||
| const { result } = renderHook(() => useLocalCredentials()); | ||
|
|
||
| await act(async () => { | ||
| await expect(result.current.setCredentials({ password: 'hunter2' })).rejects.toThrow( | ||
| 'an identifier should already be set', | ||
| ); | ||
| }); | ||
|
|
||
| expect(result.current.hasCredentials).toBe(false); | ||
| expect(mocks.store.has(passwordKey)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: clerk/javascript
Length of output: 13719
🏁 Script executed:
Repository: clerk/javascript
Length of output: 38745
🏁 Script executed:
Repository: clerk/javascript
Length of output: 41687
Make credential persistence rollback-safe.
setCredentials()writes the protected password before the supplied identifier. If the identifier write rejects, the old identifier remains paired with the new password. Write the identifier first, then the protected password. If the password write rejects, restore the previous identifier or delete it when none existed. This preserves biometric-cancellation behavior.Add tests for identifier-write failure and biometric cancellation after the identifier write.
Suggested fix
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping it as is. The identifier write has no biometric prompt, so if it fails it's a storage error, not a cancel. And the next
setCredentials()orclearCredentials()overwrites both keys anyway 🤷🏼There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wobsoriano, agreed that an identifier-write failure is not a biometric cancellation case.The concern is the state after that storage failure.
pkeycan contain the new password whilekeystill contains the old identifier.authenticate()can then fail until a latersetCredentials()orclearCredentials()call succeeds.I will leave the finding open because this is a recoverable, but user-visible, inconsistent storage state. If accepting that state is intentional, no change is required.
You are interacting with an AI system.