diff --git a/.changeset/fix-csr-multisession-impersonation.md b/.changeset/fix-csr-multisession-impersonation.md new file mode 100644 index 00000000000..48f4e114f79 --- /dev/null +++ b/.changeset/fix-csr-multisession-impersonation.md @@ -0,0 +1,6 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix impersonation in CSR multi-session apps when admin has existing session + diff --git a/packages/clerk-js/src/core/clerk.ts b/packages/clerk-js/src/core/clerk.ts index da8f4f62bb6..8a1333a7566 100644 --- a/packages/clerk-js/src/core/clerk.ts +++ b/packages/clerk-js/src/core/clerk.ts @@ -1456,7 +1456,14 @@ export class Clerk implements ClerkInterface { } if (typeof session === 'string') { - session = (this.client.sessions.find(x => x.id === session) as SignedInSessionResource) || null; + const sessionId = session; + session = (this.client.sessions.find(x => x.id === sessionId) as SignedInSessionResource) || null; + // If session not found, reload client to fetch newly created sessions (e.g., impersonation sessions) + // This handles the case where setActive is called immediately after creating a session via ticket + if (!session) { + await this.client.fetch(); + session = (this.client.sessions.find(x => x.id === sessionId) as SignedInSessionResource) || null; + } } const onBeforeSetActive: SetActiveHook =