Skip to content

Commit

Permalink
fix(clerk-js): Force devBrowser to create suffixed cookies (#4776)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Dec 16, 2024
1 parent dcd2f39 commit fa82b43
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/shy-steaks-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/clerk-js': patch
---

fix: Properly detect and create devBrowser when the suffixed version is missing but an unsuffixed version exists

If the __clerk_db_jwt referred to a different instance, we’d fetch `/environment` and `/client` with mismatched publishable keys and JWTs, breaking the app.
9 changes: 7 additions & 2 deletions packages/clerk-js/src/core/auth/cookies/devBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSecureAttribute } from '../getSecureAttribute';

export type DevBrowserCookieHandler = {
set: (jwt: string) => void;
get: () => string | undefined;
get: (mode?: 'only-suffixed') => string | undefined;
remove: () => void;
};

Expand All @@ -22,7 +22,12 @@ export const createDevBrowserCookie = (cookieSuffix: string): DevBrowserCookieHa
const devBrowserCookie = createCookieHandler(DEV_BROWSER_JWT_KEY);
const suffixedDevBrowserCookie = createCookieHandler(getSuffixedCookieName(DEV_BROWSER_JWT_KEY, cookieSuffix));

const get = () => suffixedDevBrowserCookie.get() || devBrowserCookie.get();
const get = (mode?: 'only-suffixed') => {
if (mode === 'only-suffixed') {
return suffixedDevBrowserCookie.get();
}
return suffixedDevBrowserCookie.get() || devBrowserCookie.get();
};

const set = (jwt: string) => {
const expires = addYears(Date.now(), 1);
Expand Down
4 changes: 2 additions & 2 deletions packages/clerk-js/src/core/auth/devBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export function createDevBrowser({ cookieSuffix, frontendApi, fapiClient }: Crea
return;
}

// 2. If no JWT is found in the first step, check if a JWT is already available in the __clerk_db_jwt JS cookie
if (devBrowserCookie.get()) {
// 2. If no JWT is found in the first step, check if a JWT is already available in the suffixed __clerk_db_jwt JS cookie
if (devBrowserCookie.get('only-suffixed')) {
return;
}

Expand Down

0 comments on commit fa82b43

Please sign in to comment.