Skip to content

Commit 8f9f481

Browse files
authored
fix(expo): refresh native after a JS change that lands during a native pull (#10431)
1 parent 8cd222f commit 8f9f481

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/expo': patch
3+
---
4+
5+
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.

‎packages/expo/src/provider/__tests__/nativeClientSyncBehavior/nativeClientSync.behavior.test.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe('native client sync behavior', () => {
213213

214214
await expectConverged(h);
215215
expect(server.requestCount('js', 'GET /client') - jsClientFetchesBefore).toBe(1);
216-
expect(h.native.clientRefreshCount - nativeRefreshesBefore).toBe(0);
216+
expect(h.native.clientRefreshCount - nativeRefreshesBefore).toBe(1);
217217
});
218218
});
219219

@@ -307,6 +307,29 @@ describe('native client sync behavior', () => {
307307
await expectConverged(h);
308308
});
309309

310+
it('refreshes native after a JS sign-in that lands while JS refetches a native change', async () => {
311+
const { server, token } = seedDevice();
312+
const h = await start({ server, jsDeviceToken: token, nativeDeviceToken: token });
313+
const client = h.js.client;
314+
if (!client) {
315+
throw new Error('JS has no client');
316+
}
317+
const fetchClient = client.fetch;
318+
let jsSessionId: string | undefined;
319+
client.fetch = async options => {
320+
client.fetch = fetchClient;
321+
jsSessionId = await h.js.signIn('user_2');
322+
return fetchClient(options);
323+
};
324+
325+
await h.run(() => h.native.signIn('user_1'));
326+
await h.settle();
327+
328+
expect(h.js.session?.id).toBe(jsSessionId);
329+
expect(h.native.activeSessionId).toBe(jsSessionId);
330+
await expectConverged(h);
331+
});
332+
310333
it('settles a JS change without an echo loop', async () => {
311334
const { server, token, sessionIds } = seedDevice(['user_1', 'user_2'], 'user_1');
312335
const h = await start({ server, jsDeviceToken: token, nativeDeviceToken: token });

‎packages/expo/src/provider/nativeClientSync.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,11 @@ export function createNativeClientSync(
271271
if (await ready()) {
272272
await pull();
273273
await reconciling;
274-
lastFingerprint = fingerprint(clerk);
275274
}
276275
});
277276

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

0 commit comments

Comments
 (0)