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
5 changes: 5 additions & 0 deletions .changeset/expo-refresh-native-after-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/expo': patch
---

Fix native components such as `UserButton` occasionally staying signed out after a JS sign-in. When the sign-in finished while the SDK was syncing a change from the native SDK, the native SDK was never refreshed with the new session.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('native client sync behavior', () => {

await expectConverged(h);
expect(server.requestCount('js', 'GET /client') - jsClientFetchesBefore).toBe(1);
expect(h.native.clientRefreshCount - nativeRefreshesBefore).toBe(0);
expect(h.native.clientRefreshCount - nativeRefreshesBefore).toBe(1);
});
});

Expand Down Expand Up @@ -307,6 +307,29 @@ describe('native client sync behavior', () => {
await expectConverged(h);
});

it('refreshes native after a JS sign-in that lands while JS refetches a native change', async () => {
const { server, token } = seedDevice();
const h = await start({ server, jsDeviceToken: token, nativeDeviceToken: token });
const client = h.js.client;
if (!client) {
throw new Error('JS has no client');
}
const fetchClient = client.fetch;
let jsSessionId: string | undefined;
client.fetch = async options => {
client.fetch = fetchClient;
jsSessionId = await h.js.signIn('user_2');
return fetchClient(options);
};

await h.run(() => h.native.signIn('user_1'));
await h.settle();

expect(h.js.session?.id).toBe(jsSessionId);
expect(h.native.activeSessionId).toBe(jsSessionId);
await expectConverged(h);
});

it('settles a JS change without an echo loop', async () => {
const { server, token, sessionIds } = seedDevice(['user_1', 'user_2'], 'user_1');
const h = await start({ server, jsDeviceToken: token, nativeDeviceToken: token });
Expand Down
3 changes: 1 addition & 2 deletions packages/expo/src/provider/nativeClientSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,11 @@ export function createNativeClientSync(
if (await ready()) {
await pull();
Comment thread
mikepitre marked this conversation as resolved.
await reconciling;
lastFingerprint = fingerprint(clerk);
}
});

const toNative = singleFlight(async () => {
// Let an in-flight native pull settle first so its own emissions are not echoed back.
// Let an in-flight native pull settle first so one refresh covers what it applied.
await fromNative.current()?.catch(noop);
const next = fingerprint(clerk);
if (next === lastFingerprint || !(await ready())) {
Expand Down
Loading