Skip to content

Commit ba2a15b

Browse files
brkalowclaude
andcommitted
fix: Re-write cookies after Environment resolves to apply correct partitioned attributes
Dev browser cookies are written at Step 0 before Environment is fetched, so they initially use stale (non-partitioned) attributes. After Environment resolves, refreshCookies() re-writes them with the correct attributes. Also fixes non-partitioned cookie cleanup: when transitioning to partitioned, the old non-partitioned cookies are now properly removed (plain remove without partitioned attribute targets the non-partitioned version, since the browser treats them as different cookies). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f4dd805 commit ba2a15b

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ export class AuthCookieService {
7575

7676
eventBus.on(events.UserSignOut, () => this.handleSignOut());
7777

78+
// After Environment resolves, re-write dev browser cookies with correct
79+
// partitioned attributes. Dev browser cookies are initially written before
80+
// Environment is fetched, so they may have stale attributes.
81+
eventBus.on(events.EnvironmentUpdate, () => {
82+
this.devBrowser.refreshCookies();
83+
});
84+
7885
this.refreshTokenOnFocus();
7986
this.startPollingForToken();
8087

packages/clerk-js/src/core/auth/cookies/devBrowser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export const createDevBrowserCookie = (
4444
const expires = addYears(Date.now(), 1);
4545
const { sameSite, secure, partitioned } = getCookieAttributes(options);
4646

47+
// If setting Partitioned to true, remove the existing non-partitioned cookies.
48+
// A partitioned and non-partitioned cookie with the same name are treated as
49+
// different cookies by the browser, so we need to explicitly remove the old
50+
// non-partitioned versions. Plain remove() (without attributes) targets them.
51+
if (partitioned) {
52+
suffixedDevBrowserCookie.remove();
53+
devBrowserCookie.remove();
54+
}
55+
4756
suffixedDevBrowserCookie.set(jwt, { expires, sameSite, secure, partitioned });
4857
devBrowserCookie.set(jwt, { expires, sameSite, secure, partitioned });
4958
};

packages/clerk-js/src/core/auth/cookies/session.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ export const createSessionCookie = (cookieSuffix: string, options: SessionCookie
4545
const expires = addYears(Date.now(), 1);
4646
const { sameSite, secure, partitioned } = getCookieAttributes(options);
4747

48-
// If setting Partitioned to true, remove the existing session cookies.
49-
// This is to avoid conflicts with the same cookie name without Partitioned attribute.
48+
// If setting Partitioned to true, remove the existing non-partitioned cookies.
49+
// A partitioned and non-partitioned cookie with the same name are treated as
50+
// different cookies by the browser, so we need to explicitly remove the old
51+
// non-partitioned versions. Plain remove() (without attributes) targets them.
5052
if (partitioned) {
51-
remove();
53+
sessionCookie.remove();
54+
suffixedSessionCookie.remove();
5255
}
5356

5457
sessionCookie.set(token, { expires, sameSite, secure, partitioned });

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface DevBrowser {
1818
setDevBrowserJWT(jwt: string): void;
1919

2020
removeDevBrowserJWT(): void;
21+
22+
refreshCookies(): void;
2123
}
2224

2325
export type CreateDevBrowserOptions = {
@@ -106,11 +108,19 @@ export function createDevBrowser({
106108
setDevBrowserJWT(data?.id);
107109
}
108110

111+
function refreshCookies() {
112+
const jwt = getDevBrowserJWT();
113+
if (jwt) {
114+
setDevBrowserJWT(jwt);
115+
}
116+
}
117+
109118
return {
110119
clear,
111120
setup,
112121
getDevBrowserJWT,
113122
setDevBrowserJWT,
114123
removeDevBrowserJWT,
124+
refreshCookies,
115125
};
116126
}

0 commit comments

Comments
 (0)