diff --git a/packages/client-runtime/src/relay/cloudAccountService.test.ts b/packages/client-runtime/src/relay/cloudAccountService.test.ts index f66d8a9423f25..1549f9e4f6a62 100644 --- a/packages/client-runtime/src/relay/cloudAccountService.test.ts +++ b/packages/client-runtime/src/relay/cloudAccountService.test.ts @@ -6,7 +6,7 @@ import { planCloudAccountStep, } from "./cloudAccountService.ts"; -type Emit = (user: { readonly id: string } | null | undefined) => void; +type Emit = (user: { readonly id: string } | null | undefined, clientId?: string) => void; function makeSource() { let listener: Parameters[0] | null = null; @@ -18,7 +18,8 @@ function makeSource() { }; }, }; - const emit: Emit = (user) => listener?.({ user }); + const emit: Emit = (user, clientId = "client_1") => + listener?.({ user, client: { id: clientId } }); return { source, emit, isSubscribed: () => listener !== null }; } @@ -154,6 +155,32 @@ describe("createCloudAccountService", () => { expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]); }); + it("waits for Clerk's API to confirm a signed-out launch before cleaning up", async () => { + const { service, log, nextActivation, nextCleanUp } = makeService(); + const { source, emit } = makeSource(); + service.attach(source); + + // Offline: an id-less client, or the placeholder built from the session cookie. + emit(null, ""); + emit(null, "client_init"); + expect(service.getState()).toEqual({ _tag: "Unobserved" }); + expect(log).toEqual([]); + + const cleanUp = nextCleanUp(); + emit(null); + await cleanUp; + expect(log).toEqual(["deactivate", "cleanUp:null:false"]); + + const activation = nextActivation(); + emit({ id: "a" }); + await activation; + log.length = 0; + const signOut = nextCleanUp(); + emit(null, "client_init"); + await signOut; + expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]); + }); + it("never activates an account that signed out while it was activating", async () => { const { service, log, nextCleanUp, gate } = makeService(); const { source, emit } = makeSource(); diff --git a/packages/client-runtime/src/relay/cloudAccountService.ts b/packages/client-runtime/src/relay/cloudAccountService.ts index 6592fba687b78..7f885281d3b90 100644 --- a/packages/client-runtime/src/relay/cloudAccountService.ts +++ b/packages/client-runtime/src/relay/cloudAccountService.ts @@ -7,10 +7,22 @@ import { reportAtomCommandResult, settlePromise } from "../state/runtime.ts"; */ export interface CloudAccountSource { readonly addListener: ( - listener: (resources: { readonly user?: { readonly id: string } | null | undefined }) => void, + listener: (resources: { + readonly client?: { readonly id?: string | null | undefined } | null | undefined; + readonly user?: { readonly id: string } | null | undefined; + }) => void, ) => () => void; } +/** + * Clerk's load status cannot prove its API answered: offline it emits an + * id-less client, and a failed launch fetch emits a "client_init" placeholder + * built from the session cookie. Only a real /v1/client response has an id. + */ +const isClientFromClerkApi = ( + client: { readonly id?: string | null | undefined } | null | undefined, +) => Boolean(client?.id) && client?.id !== "client_init"; + interface CloudAccountSignIn { readonly _tag: "SignIn"; readonly accountId: string; @@ -32,7 +44,8 @@ export type CloudAccountStep = /** * `previous` is undefined until Clerk's first emission. A first emission that * is signed out still cleans up, so data left by an account that signed out - * while the app was closed does not linger. + * while the app was closed does not linger. The service only plans that step + * once Clerk's API has confirmed the sign-out. */ export function planCloudAccountStep( previous: string | null | undefined, @@ -200,7 +213,17 @@ export function createCloudAccountService( // Clerk reports `user: undefined` until it loads; pending sessions // still carry a user and count as signed in. if (resources.user === undefined) return; - observe(source, resources.user?.id ?? null); + const next = resources.user?.id ?? null; + // An unreachable Clerk API at launch looks signed out; stay unobserved + // until Clerk confirms it, so saved environments survive offline starts. + if ( + next === null && + state._tag === "Unobserved" && + !isClientFromClerkApi(resources.client) + ) { + return; + } + observe(source, next); }), }; attached = current;