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
5 changes: 5 additions & 0 deletions .changeset/wide-bags-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fixes a scenario where the session token would not immediately update after a call to `Clerk.session.touch()`.
2 changes: 1 addition & 1 deletion packages/clerk-js/bundlewatch.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": [
{ "path": "./dist/clerk.js", "maxSize": "610kB" },
{ "path": "./dist/clerk.browser.js", "maxSize": "70.16KB" },
{ "path": "./dist/clerk.browser.js", "maxSize": "70.2KB" },
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "113KB" },
{ "path": "./dist/clerk.headless*.js", "maxSize": "53.06KB" },
{ "path": "./dist/ui-common*.js", "maxSize": "108.4KB" },
Expand Down
6 changes: 6 additions & 0 deletions packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export class Session extends BaseResource implements SessionResource {
return this._basePost({
action: 'touch',
body: { active_organization_id: this.lastActiveOrganizationId },
}).then(res => {
// touch() will potentially change the session state, and so we need to ensure we emit the updated token that comes back in the response. This avoids potential issues where the session cookie is out of sync with the current session state.
if (res.lastActiveToken) {
eventBus.emit(events.TokenUpdate, { token: res.lastActiveToken });
}
return res;
});
};

Expand Down
40 changes: 40 additions & 0 deletions packages/clerk-js/src/core/resources/__tests__/Session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,46 @@ describe('Session', () => {
});
});

describe('touch()', () => {
let dispatchSpy: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
dispatchSpy = vi.spyOn(eventBus, 'emit');
BaseResource.clerk = clerkMock() as any;
});

afterEach(() => {
dispatchSpy?.mockRestore();
BaseResource.clerk = null as any;
});

it('dispatches token:update event on touch', async () => {
const mockToken = { object: 'token', jwt: mockJwt };
const session = new Session({
status: 'active',
id: 'session_1',
object: 'session',
user: createUser({}),
last_active_organization_id: null,
actor: null,
created_at: new Date().getTime(),
updated_at: new Date().getTime(),
last_active_token: mockToken,
} as SessionJSON);

(BaseResource.clerk.getFapiClient().request as Mock).mockResolvedValue({
payload: session,
});

await session.touch();

expect(dispatchSpy).toHaveBeenCalledTimes(1);
expect(dispatchSpy).toHaveBeenCalledWith('token:update', {
token: session.lastActiveToken,
});
});
});

describe('isAuthorized()', () => {
it('user with permission to delete the organization should be able to delete the organization', async () => {
const session = new Session({
Expand Down
Loading