Skip to content
Open
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
16 changes: 12 additions & 4 deletions packages/clerk-js/src/core/tokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ interface SessionTokenEvent {
traceId: string;
}

const generateTabId = (): string => {
return Math.random().toString(36).slice(2);
};

/**
* Creates an in-memory token cache with optional BroadcastChannel synchronization across tabs.
* Automatically manages token expiration and cleanup via scheduled timeouts.
* BroadcastChannel support is enabled only in the channel build variant.
*/
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
const cache = new Map<string, TokenCacheValue>();
const tabId = generateTabId();

let broadcastChannel: BroadcastChannel | null = null;

Expand Down Expand Up @@ -213,6 +218,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
expectedTokenId,
organizationId: data.organizationId,
receivedTokenId: data.tokenId,
tabId,
template: data.template,
traceId: data.traceId,
},
Expand All @@ -227,7 +233,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
} catch (error) {
debugLogger.warn(
'Failed to parse token from broadcast, skipping cache update',
{ error, tokenId: data.tokenId, traceId: data.traceId },
{ error, tabId, tokenId: data.tokenId, traceId: data.traceId },
'tokenCache',
);
return;
Expand All @@ -238,7 +244,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
if (!iat || !exp) {
debugLogger.warn(
'Token missing iat/exp claim, skipping cache update',
{ tokenId: data.tokenId, traceId: data.traceId },
{ tabId, tokenId: data.tokenId, traceId: data.traceId },
'tokenCache',
);
return;
Expand All @@ -252,7 +258,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
if (existingIat && existingIat >= iat) {
debugLogger.debug(
'Ignoring older token broadcast',
{ existingIat, incomingIat: iat, tokenId: data.tokenId, traceId: data.traceId },
{ existingIat, incomingIat: iat, tabId, tokenId: data.tokenId, traceId: data.traceId },
'tokenCache',
);
return;
Expand All @@ -261,7 +267,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
} catch (error) {
debugLogger.warn(
'Existing entry compare failed; proceeding with broadcast update',
{ error, tokenId: data.tokenId, traceId: data.traceId },
{ error, tabId, tokenId: data.tokenId, traceId: data.traceId },
'tokenCache',
);
}
Expand All @@ -271,6 +277,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
{
iat,
organizationId: data.organizationId,
tabId,
template: data.template,
tokenId: data.tokenId,
traceId: data.traceId,
Expand Down Expand Up @@ -362,6 +369,7 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
{
organizationId,
sessionId,
tabId,
template,
tokenId: entry.tokenId,
traceId,
Expand Down
Loading