Skip to content

Commit c5d0152

Browse files
fix(clerk-js): Set __session and __client_uat before invalidating cache (#3774)
Co-authored-by: Lennart <lekoarts@gmail.com>
1 parent 76479c0 commit c5d0152

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

.changeset/poor-rockets-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/clerk-js": patch
3+
---
4+
5+
Set `session` and `client_uat` before invalidating cache during `setActive()` flow

packages/clerk-js/src/core/__tests__/clerk.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe('Clerk singleton', () => {
156156
user: {},
157157
touch: jest.fn(),
158158
getToken: jest.fn(),
159+
lastActiveToken: { getRawString: () => 'mocked-token' },
159160
};
160161
let evenBusSpy;
161162

@@ -243,6 +244,19 @@ describe('Clerk singleton', () => {
243244
await sut.setActive({ session: null });
244245
});
245246

247+
it('sets __session and __client_uat cookie before calling __unstable__onBeforeSetActive', async () => {
248+
mockSession.touch.mockReturnValueOnce(Promise.resolve());
249+
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession] }));
250+
251+
(window as any).__unstable__onBeforeSetActive = () => {
252+
expect(evenBusSpy).toHaveBeenCalledWith('token:update', { token: mockSession.lastActiveToken });
253+
};
254+
255+
const sut = new Clerk(productionPublishableKey);
256+
await sut.load();
257+
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
258+
});
259+
246260
it('calls __unstable__onAfterSetActive after beforeEmit and session.touch', async () => {
247261
const beforeEmitMock = jest.fn();
248262
mockSession.touch.mockReturnValueOnce(Promise.resolve());

packages/clerk-js/src/core/clerk.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ export class Clerk implements ClerkInterface {
720720
eventBus.dispatch(events.TokenUpdate, { token: null });
721721
}
722722

723+
if (session?.lastActiveToken) {
724+
eventBus.dispatch(events.TokenUpdate, { token: session.lastActiveToken });
725+
}
726+
723727
await onBeforeSetActive();
724728

725729
//1. setLastActiveSession to passed user session (add a param).
@@ -755,6 +759,7 @@ export class Clerk implements ClerkInterface {
755759
}
756760

757761
this.#setAccessors(newSession);
762+
758763
this.#emit();
759764
await onAfterSetActive();
760765
this.#resetComponentsState();

0 commit comments

Comments
 (0)