Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/yellow-radios-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/clerk-js': patch
---

Fix `touchSession` option to only affect session touch behavior to window focus events.

Previously, when `touchSession: false` was provided, it incorrectly prevented session touching during `setActive()` calls when switching sessions or selecting organizations.
32 changes: 8 additions & 24 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,15 @@ describe('Clerk singleton', () => {
expect(mockSession.touch).toHaveBeenCalled();
});

it('does not call session.touch if Clerk was initialised with touchSession set to false', async () => {
mockSession.touch.mockReturnValueOnce(Promise.resolve());
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
mockSession.getToken.mockResolvedValue('mocked-token');
describe('with `touchSession` set to false', () => {
it('calls session.touch by default outside of focus window event', async () => {
mockSession.touch.mockReturnValue(Promise.resolve());
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));

const sut = new Clerk(productionPublishableKey);
await sut.load({ touchSession: false });
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
await waitFor(() => {
expect(mockSession.touch).not.toHaveBeenCalled();
expect(mockSession.getToken).toHaveBeenCalled();
const sut = new Clerk(productionPublishableKey);
await sut.load({ touchSession: false });
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
expect(mockSession.touch).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -525,20 +523,6 @@ describe('Clerk singleton', () => {
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
expect(mockSession.touch).toHaveBeenCalled();
});

it('does not call session.touch if Clerk was initialised with touchSession set to false', async () => {
mockSession.touch.mockReturnValueOnce(Promise.resolve());
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
mockSession.getToken.mockResolvedValue('mocked-token');

const sut = new Clerk(productionPublishableKey);
await sut.load({ touchSession: false });
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
await waitFor(() => {
expect(mockSession.touch).not.toHaveBeenCalled();
expect(mockSession.getToken).toHaveBeenCalled();
});
});
});

describe('with force organization selection enabled', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,9 @@ export class Clerk implements ClerkInterface {
}
this.#touchThrottledUntil = Date.now() + 5_000;

void this.#touchCurrentSession(this.session);
if (this.#options.touchSession) {
void this.#touchCurrentSession(this.session);
}
});

/**
Expand Down Expand Up @@ -2648,7 +2650,7 @@ export class Clerk implements ClerkInterface {

// TODO: Be more conservative about touches. Throttle, don't touch when only one user, etc
#touchCurrentSession = async (session?: SignedInSessionResource | null): Promise<void> => {
if (!session || !this.#options.touchSession) {
if (!session) {
return Promise.resolve();
}

Expand Down