Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
4cb7abf
test(expo): cover a slow native configure leaving JS on another client
wobsoriano Dec 5, 2026
f3ed4fc
fix(expo): converge on the native client after a slow native configure
wobsoriano Dec 5, 2026
620f758
refactor(expo): name the slow-configure helpers instead of commenting…
wobsoriano Dec 5, 2026
500512c
fix(expo): refetch the JS client once per native client while converging
wobsoriano Dec 5, 2026
c8e3fee
fix(expo): stop applying a native client pull that started before a J…
wobsoriano Dec 5, 2026
0b4a39b
fix(expo): converge on the native client after a failed native device…
wobsoriano Dec 5, 2026
3fc4ac4
fix(expo): keep the app token cache on the native device token
wobsoriano Dec 5, 2026
7d5a860
test(expo): cover a native sign-in restored after a slow native confi…
wobsoriano Dec 5, 2026
3d9fa66
fix(expo): skip convergence when JS signs out while it reads the nati…
wobsoriano Dec 5, 2026
3493b64
fix(expo): keep native repair when signing out of an inactive session
wobsoriano Dec 5, 2026
b81745d
test(expo): cover sign-out during a convergence activation and a late…
wobsoriano Dec 5, 2026
1eaa56d
test(expo): keep native configure working when the slow configure fla…
wobsoriano Dec 5, 2026
6e35915
test(expo): remove the slow configure verify hook from the test app
wobsoriano Dec 5, 2026
3c10249
fix(expo): converge on the native client only while JS has no session
wobsoriano Dec 5, 2026
49c0c36
fix(expo): drop a native pull response that lands after a JS sign-out
wobsoriano Dec 5, 2026
162120d
fix(expo): keep a native change pending across a ClerkProvider remount
wobsoriano Dec 5, 2026
605c24e
fix(expo): keep a remount's pending repair when an earlier mount's pu…
wobsoriano Dec 5, 2026
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-converge-native-client-slow-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/expo': patch
---

Fix the JS SDK staying signed out while native components are signed in after a slow app start. When the native SDK takes longer than usual to start, the JS SDK now switches to the native SDK's client and session once it is ready, so native sign-in no longer fails with `session_exists`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FakeClerkServer, FakeServerClientJSON, FapiResponse } from './fakeServer';

type FakeRequestInit = { url: URL; headers: Headers };
type FakeRequestInit = { url: URL; headers: Headers; signal?: AbortSignal };
type BeforeRequestHook = (requestInit: FakeRequestInit) => unknown;
type FakePayload = { client?: FakeServerClientJSON | null; response?: FakeServerClientJSON | null };
type AfterResponseHook = (
Expand Down Expand Up @@ -54,8 +54,12 @@ export class FakeClientResource {
return this.sessions.filter(session => session.status === 'active');
}

fetch = async (_options?: { fetchMaxTries?: number }): Promise<FakeClientResource> => {
const response = await this.clerk.fapi(token => this.clerk.server.getClient('js', token), 'response');
fetch = async (options?: { fetchMaxTries?: number; abortSignal?: AbortSignal }): Promise<FakeClientResource> => {
const response = await this.clerk.fapi(
token => this.clerk.server.getClient('js', token),
'response',
options?.abortSignal,
);
return this.fromJSON(response.client);
};

Expand Down Expand Up @@ -114,8 +118,9 @@ export class FakeClerk {
async fapi<T extends FapiResponse>(
request: (token: string | null) => Promise<T>,
clientField: 'client' | 'response' = 'client',
signal?: AbortSignal,
): Promise<T> {
const requestInit = await this.#prepareRequest();
const requestInit = await this.#prepareRequest(signal);
const response = await request(requestInit.headers.get('authorization') || null);
const headers = new Headers(response.token ? { authorization: response.token } : {});
const payload: FakePayload = { [clientField]: response.client };
Expand All @@ -130,8 +135,8 @@ export class FakeClerk {
return requestInit.headers.get('authorization') || null;
}

async #prepareRequest(): Promise<FakeRequestInit> {
const requestInit = { url: new URL('https://fapi.clerk.test/v1/client'), headers: new Headers() };
async #prepareRequest(signal?: AbortSignal): Promise<FakeRequestInit> {
const requestInit = { url: new URL('https://fapi.clerk.test/v1/client'), headers: new Headers(), signal };
for (const hook of this.#beforeRequestHooks) {
await hook(requestInit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class FakeNativeClerk {
client: FakeServerClientJSON | null = null;
isConfigured = false;
clientRefreshCount = 0;
deviceTokenWritesFail = false;
deviceTokenReadFailures = 0;
#listeners = new Set<NativeClientChangeListener>();
#heldChanges: Array<[NativeClientChange, NativeChangeOrigin]> | null = null;
#startupGate: Promise<void> | null = null;
Expand Down Expand Up @@ -142,6 +144,10 @@ export class FakeNativeClerk {
await this.#observe(() => this.server.getClient('native', this.token));
}

restoreClientFromKeychain(client: FakeServerClientJSON | null): void {
this.client = client;
}

/** Native ends up on a brand new client with no sessions, e.g. after losing its keychain entry. */
async switchToForeignSessionlessClient(): Promise<void> {
await this.#observe(() => this.server.createSessionlessClient('native'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ export class FakeClerkServer {
return { client, token: this.#mintToken(client.id) };
}

createClientForMissingToken = false;
omitUnrotatedTokenOnReadAndTouch = false;

#authenticate(token: string | null): { client: FakeServerClientJSON | null; token: string | null } {
if (token && this.#retiredTokens.has(token)) {
if ((token && this.#retiredTokens.has(token)) || (!token && this.createClientForMissingToken)) {
return this.#createClient();
}
return { client: this.#clientFor(token), token };
Expand All @@ -123,6 +126,14 @@ export class FakeClerkServer {
return { status, client: client ? cloneClient(client) : null, token: responseToken };
}

#withoutUnrotatedToken<T extends FapiResponse>(operation: string, token: string | null, response: T): T {
const omitsToken =
this.omitUnrotatedTokenOnReadAndTouch &&
(operation === 'GET /client' || operation === 'POST /client/sessions/:id/touch') &&
response.token === token;
return omitsToken ? { ...response, token: null } : response;
}

async #send<T extends FapiResponse>(
caller: FapiCaller,
operation: string,
Expand All @@ -141,7 +152,7 @@ export class FakeClerkServer {
}
throw new TypeError('Network request failed');
}
const response = handle();
const response = this.#withoutUnrotatedToken(operation, token, handle());
const holdIndex = this.#responseHolds.findIndex(hold => hold.caller === caller && hold.operation === operation);
if (holdIndex >= 0) {
const [hold] = this.#responseHolds.splice(holdIndex, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type NativeSyncScenario = {
/** The native SDK fails to configure, e.g. a native build that cannot initialize Clerk. */
failNativeConfigure?: boolean;
delayNativeStartup?: boolean;
delayNativeConfigure?: boolean;
skipInitialLoad?: boolean;
strictMode?: boolean;
};

Expand All @@ -33,6 +35,8 @@ export type NativeSyncHarness = {
run: <T>(action: () => T | Promise<T>) => Promise<T>;
settle: () => Promise<void>;
releaseNativeStartup: () => void;
releaseNativeConfigure: () => void;
remount: () => void;
unmount: () => void;
};

Expand Down
Loading
Loading