Skip to content

Commit 4602f18

Browse files
authored
fix(clerk-js): Update touchSession option to only affect window focus events (#6444)
1 parent a3355af commit 4602f18

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

.changeset/yellow-radios-stop.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Fix `touchSession` option to only affect session touch behavior to window focus events.
6+
7+
Previously, when `touchSession: false` was provided, it incorrectly prevented session touching during `setActive()` calls when switching sessions or selecting organizations.

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,15 @@ describe('Clerk singleton', () => {
205205
expect(mockSession.touch).toHaveBeenCalled();
206206
});
207207

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

213-
const sut = new Clerk(productionPublishableKey);
214-
await sut.load({ touchSession: false });
215-
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
216-
await waitFor(() => {
217-
expect(mockSession.touch).not.toHaveBeenCalled();
218-
expect(mockSession.getToken).toHaveBeenCalled();
213+
const sut = new Clerk(productionPublishableKey);
214+
await sut.load({ touchSession: false });
215+
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
216+
expect(mockSession.touch).toHaveBeenCalled();
219217
});
220218
});
221219

@@ -525,20 +523,6 @@ describe('Clerk singleton', () => {
525523
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
526524
expect(mockSession.touch).toHaveBeenCalled();
527525
});
528-
529-
it('does not call session.touch if Clerk was initialised with touchSession set to false', async () => {
530-
mockSession.touch.mockReturnValueOnce(Promise.resolve());
531-
mockClientFetch.mockReturnValue(Promise.resolve({ signedInSessions: [mockSession] }));
532-
mockSession.getToken.mockResolvedValue('mocked-token');
533-
534-
const sut = new Clerk(productionPublishableKey);
535-
await sut.load({ touchSession: false });
536-
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
537-
await waitFor(() => {
538-
expect(mockSession.touch).not.toHaveBeenCalled();
539-
expect(mockSession.getToken).toHaveBeenCalled();
540-
});
541-
});
542526
});
543527

544528
describe('with force organization selection enabled', () => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,9 @@ export class Clerk implements ClerkInterface {
26172617
}
26182618
this.#touchThrottledUntil = Date.now() + 5_000;
26192619

2620-
void this.#touchCurrentSession(this.session);
2620+
if (this.#options.touchSession) {
2621+
void this.#touchCurrentSession(this.session);
2622+
}
26212623
});
26222624

26232625
/**
@@ -2648,7 +2650,7 @@ export class Clerk implements ClerkInterface {
26482650

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

0 commit comments

Comments
 (0)