Skip to content

Commit b302647

Browse files
committed
fix(expo): preserve unreadable client tokens in app extensions
1 parent 3457616 commit b302647

3 files changed

Lines changed: 49 additions & 27 deletions

File tree

‎.changeset/keychain-read-keeps-client.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
Fix iOS users being signed out when the keychain can't be read for a moment, for example when the app is launched in the background before the device has been unlocked since a restart.
66

7-
- On iOS, `getToken()` from the `@clerk/expo/token-cache` `tokenCache` now rejects with the keychain error while the app is in the background or inactive, instead of deleting the stored client token and returning `null`. A read that fails while the app is in the foreground still clears the token, as before.
7+
- On iOS, `getToken()` from the `@clerk/expo/token-cache` `tokenCache` now rejects with the keychain error unless the read both starts and fails while the app is active, instead of deleting the stored client token and returning `null`. This also preserves tokens in app extensions. A read that both starts and fails while the app is active still clears the token, as before.
88
- When reading the client token fails, from this `tokenCache` or a custom one, Clerk now waits until the app is active and reads it again before sending the request. If this happens while the app is in the background, those requests wait until the app is opened. If a custom `tokenCache` still fails, the request fails with its error, as before.
9-
- A custom `tokenCache` that deletes the token and returns `null` whenever a read fails will still sign users out in this situation. To avoid that, rethrow the error while `AppState.currentState` is `background` or `inactive`, and keep deleting the token when a read fails in the foreground. A custom `tokenCache` that always rethrows makes Clerk requests fail until the token can be read again.
9+
- A custom `tokenCache` that deletes the token and returns `null` whenever a read fails will still sign users out in this situation. To avoid that, rethrow the error unless `AppState.currentState` is `active` both when the read starts and when it fails. A custom `tokenCache` that always rethrows makes Clerk requests fail until the token can be read again.

‎packages/expo/src/token-cache/__tests__/tokenCache.test.ts‎

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('tokenCache', () => {
5959
expect(mocks.deleteItemAsync).not.toHaveBeenCalled();
6060
});
6161

62-
test.each(['background', 'inactive'])(
62+
test.each(['background', 'inactive', 'extension', 'unknown', null])(
6363
'keeps the stored token and rethrows when the keychain read fails on iOS while the app is %s',
6464
async appState => {
6565
const readError = new Error('User interaction is not allowed.');
@@ -72,26 +72,51 @@ describe('tokenCache', () => {
7272
},
7373
);
7474

75-
test('keeps the stored token when a read started in the background fails after the app becomes active', async () => {
76-
const readError = new Error('User interaction is not allowed.');
77-
mocks.appState.currentState = 'background';
78-
let rejectRead: (error: Error) => void = () => {};
79-
mocks.getItemAsync.mockReturnValue(
80-
new Promise<string | null>((_, reject) => {
81-
rejectRead = reject;
82-
}),
83-
);
84-
const tokenCache = await loadTokenCache();
75+
test.each(['background', 'inactive', 'extension', 'unknown', null])(
76+
'keeps the stored token when a read started in %s fails after the app becomes active',
77+
async appState => {
78+
const readError = new Error('User interaction is not allowed.');
79+
mocks.appState.currentState = appState;
80+
let rejectRead: (error: Error) => void = () => {};
81+
mocks.getItemAsync.mockReturnValue(
82+
new Promise<string | null>((_, reject) => {
83+
rejectRead = reject;
84+
}),
85+
);
86+
const tokenCache = await loadTokenCache();
8587

86-
const read = tokenCache.getToken('__clerk_client_jwt');
87-
mocks.appState.currentState = 'active';
88-
rejectRead(readError);
88+
const read = tokenCache.getToken('__clerk_client_jwt');
89+
mocks.appState.currentState = 'active';
90+
rejectRead(readError);
8991

90-
await expect(read).rejects.toBe(readError);
91-
expect(mocks.deleteItemAsync).not.toHaveBeenCalled();
92-
});
92+
await expect(read).rejects.toBe(readError);
93+
expect(mocks.deleteItemAsync).not.toHaveBeenCalled();
94+
},
95+
);
96+
97+
test.each(['background', 'inactive', 'extension', 'unknown', null])(
98+
'keeps the stored token when an active read fails after the app becomes %s',
99+
async appState => {
100+
const readError = new Error('User interaction is not allowed.');
101+
mocks.appState.currentState = 'active';
102+
let rejectRead: (error: Error) => void = () => {};
103+
mocks.getItemAsync.mockReturnValue(
104+
new Promise<string | null>((_, reject) => {
105+
rejectRead = reject;
106+
}),
107+
);
108+
const tokenCache = await loadTokenCache();
109+
110+
const read = tokenCache.getToken('__clerk_client_jwt');
111+
mocks.appState.currentState = appState;
112+
rejectRead(readError);
113+
114+
await expect(read).rejects.toBe(readError);
115+
expect(mocks.deleteItemAsync).not.toHaveBeenCalled();
116+
},
117+
);
93118

94-
test.each(['active', 'extension', null])(
119+
test.each(['active'])(
95120
'removes an unreadable token when the read fails on iOS while the app state is %s',
96121
async appState => {
97122
mocks.appState.currentState = appState;

‎packages/expo/src/token-cache/index.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as SecureStore from 'expo-secure-store';
2-
import { Platform } from 'react-native';
2+
import { AppState, Platform } from 'react-native';
33

44
import type { TokenCache } from '../cache';
5-
import { isAppInBackgroundOrInactive, isNative } from '../utils';
5+
import { isNative } from '../utils';
66

77
/**
88
* Create a token cache using Expo's SecureStore
@@ -20,15 +20,12 @@ const createTokenCache = (): TokenCache => {
2020

2121
return {
2222
getToken: async (key: string) => {
23-
const startedInBackground = isAppInBackgroundOrInactive();
23+
const startedWhileActive = AppState.currentState === 'active';
2424
try {
2525
const item = await SecureStore.getItemAsync(key, secureStoreOpts);
2626
return item;
2727
} catch (error) {
28-
// iOS keychain reads also fail temporarily, such as when the app is launched in the
29-
// background before the device has been unlocked since boot, while the stored item
30-
// is still valid. Keep it until a read fails while the app is in the foreground.
31-
if (Platform.OS === 'ios' && (startedInBackground || isAppInBackgroundOrInactive())) {
28+
if (Platform.OS === 'ios' && (!startedWhileActive || AppState.currentState !== 'active')) {
3229
throw error;
3330
}
3431
await SecureStore.deleteItemAsync(key, secureStoreOpts);

0 commit comments

Comments
 (0)