Skip to content

Commit 02ec26a

Browse files
brkalowjacekradko
andauthored
fix(clerk-js): Use a cookie instead of localStorage for active org (#4394)
Co-authored-by: Jacek Radko <[email protected]>
1 parent d58fda9 commit 02ec26a

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

.changeset/eleven-pants-juggle.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+
Use a cookie instead of localStorage for the active org ID to avoid issues when localStorage is disabled at the browser level.

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createCookieHandler } from '@clerk/shared/cookie';
12
import { setDevBrowserJWTInURL } from '@clerk/shared/devBrowser';
23
import { is4xxError, isClerkAPIResponseError, isNetworkError } from '@clerk/shared/error';
34
import type { Clerk, InstanceType } from '@clerk/types';
@@ -36,6 +37,7 @@ export class AuthCookieService {
3637
private poller: SessionCookiePoller | null = null;
3738
private clientUat: ClientUatCookieHandler;
3839
private sessionCookie: SessionCookieHandler;
40+
private activeOrgCookie: ReturnType<typeof createCookieHandler>;
3941
private devBrowser: DevBrowser;
4042

4143
public static async create(clerk: Clerk, fapiClient: FapiClient, instanceType: InstanceType) {
@@ -62,6 +64,7 @@ export class AuthCookieService {
6264

6365
this.clientUat = createClientUatCookie(cookieSuffix);
6466
this.sessionCookie = createSessionCookie(cookieSuffix);
67+
this.activeOrgCookie = createCookieHandler('clerk_active_org');
6568
this.devBrowser = createDevBrowser({
6669
frontendApi: clerk.frontendApi,
6770
fapiClient,
@@ -191,20 +194,20 @@ export class AuthCookieService {
191194
* The below methods are used to determine whether or not an unfocused tab can be responsible
192195
* for setting the session cookie. A session cookie should only be set by a tab who's selected
193196
* organization matches the session's active organization. By storing the active organization
194-
* ID in local storage, we can check the value across tabs. If a tab's organization ID does not
195-
* match the value in local storage, it is not responsible for updating the session cookie.
197+
* ID in a cookie, we can check the value across tabs. If a tab's organization ID does not
198+
* match the value in the active org cookie, it is not responsible for updating the session cookie.
196199
*/
197200

198201
public setActiveOrganizationInStorage() {
199202
if (this.clerk.organization?.id) {
200-
localStorage.setItem('clerk_active_org', this.clerk.organization.id);
203+
this.activeOrgCookie.set(this.clerk.organization.id);
201204
} else {
202-
localStorage.removeItem('clerk_active_org');
205+
this.activeOrgCookie.remove();
203206
}
204207
}
205208

206209
private isCurrentOrganizationActive() {
207-
const activeOrganizationId = localStorage.getItem('clerk_active_org');
210+
const activeOrganizationId = this.activeOrgCookie.get();
208211

209212
if (!activeOrganizationId && !this.clerk.organization?.id) {
210213
return true;

0 commit comments

Comments
 (0)