Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clerk-js): Force devBrowser to create suffixed cookies #4776

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
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
Loading