Skip to content

Commit 27d9cb6

Browse files
committed
fix(clerk-js): Keep the session cookie while cache is being invalidated
Otherwise, in Nextjs apps the invalidate cache server action will result in a 404 if current route is being protected by auth().protect()
1 parent 05bf278 commit 27d9cb6

3 files changed

Lines changed: 11 additions & 25 deletions

File tree

.changeset/sixty-kids-sparkle.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+
Fix 404s after signing out in NextJS apps by keeping the session cookie while cache is being invalidated

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,25 +225,6 @@ describe('Clerk singleton', () => {
225225
expect(mockSession.touch).toHaveBeenCalled();
226226
});
227227

228-
/**
229-
* The __session cookie needs to be cleared before calling __unstable__onBeforeSetActive
230-
* as the callback may rely on the absence of the cookie to determine the user is logged out or not
231-
* For example, for NextJS integration, calling __unstable__onBeforeSetActive before clearing the cookie
232-
* would result in hitting the middleware with a valid session cookie (until it expires), even if the session no longer exists
233-
*/
234-
it('clears __session cookie before calling __unstable__onBeforeSetActive', async () => {
235-
mockSession.touch.mockReturnValueOnce(Promise.resolve());
236-
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession] }));
237-
238-
(window as any).__unstable__onBeforeSetActive = () => {
239-
expect(eventBusSpy).toHaveBeenCalledWith('token:update', { token: null });
240-
};
241-
242-
const sut = new Clerk(productionPublishableKey);
243-
await sut.load();
244-
await sut.setActive({ session: null });
245-
});
246-
247228
it('sets __session and __client_uat cookie before calling __unstable__onBeforeSetActive', async () => {
248229
mockSession.touch.mockReturnValueOnce(Promise.resolve());
249230
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession] }));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ export class Clerk implements ClerkInterface {
731731
}
732732
}
733733

734+
if (session?.lastActiveToken) {
735+
eventBus.dispatch(events.TokenUpdate, { token: session.lastActiveToken });
736+
}
737+
738+
await onBeforeSetActive();
739+
734740
// If this.session exists, then signOut was triggered by the current tab
735741
// and should emit. Other tabs should not emit the same event again
736742
const shouldSignOutSession = this.session && newSession === null;
@@ -739,12 +745,6 @@ export class Clerk implements ClerkInterface {
739745
eventBus.dispatch(events.TokenUpdate, { token: null });
740746
}
741747

742-
if (session?.lastActiveToken) {
743-
eventBus.dispatch(events.TokenUpdate, { token: session.lastActiveToken });
744-
}
745-
746-
await onBeforeSetActive();
747-
748748
//1. setLastActiveSession to passed user session (add a param).
749749
// Note that this will also update the session's active organization
750750
// id.

0 commit comments

Comments
 (0)