Skip to content

fix(clerk-js): Set __session and __client_uat before invalidating cache #3774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/poor-rockets-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/clerk-js": patch
---

Set `session` and `client_uat` before invalidating cache during `setActive()` flow
14 changes: 14 additions & 0 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('Clerk singleton', () => {
user: {},
touch: jest.fn(),
getToken: jest.fn(),
lastActiveToken: { getRawString: () => 'mocked-token' },
};
let evenBusSpy;

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

it('sets __session and __client_uat cookie before calling __unstable__onBeforeSetActive', async () => {
mockSession.touch.mockReturnValueOnce(Promise.resolve());
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession] }));

(window as any).__unstable__onBeforeSetActive = () => {
expect(evenBusSpy).toHaveBeenCalledWith('token:update', { token: mockSession.lastActiveToken });
};

const sut = new Clerk(productionPublishableKey);
await sut.load();
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
});

it('calls __unstable__onAfterSetActive after beforeEmit and session.touch', async () => {
const beforeEmitMock = jest.fn();
mockSession.touch.mockReturnValueOnce(Promise.resolve());
Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,10 @@ export class Clerk implements ClerkInterface {
eventBus.dispatch(events.TokenUpdate, { token: null });
}

if (session?.lastActiveToken) {
eventBus.dispatch(events.TokenUpdate, { token: session.lastActiveToken });
}

await onBeforeSetActive();

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

this.#setAccessors(newSession);

this.#emit();
await onAfterSetActive();
this.#resetComponentsState();
Expand Down
Loading