Skip to content

Commit 0a42026

Browse files
committed
wip
1 parent 6c8a275 commit 0a42026

File tree

3 files changed

+216
-272
lines changed

3 files changed

+216
-272
lines changed

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

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,7 +2650,23 @@ export class Clerk implements ClerkInterface {
26502650
});
26512651

26522652
eventBus.on(events.TokenUpdate, payload => {
2653+
const tokenString = payload.token?.getRawString() || null;
2654+
2655+
// Store token in session storage per tab
2656+
try {
2657+
if (tokenString) {
2658+
sessionStorage.setItem('clerk-token', tokenString);
2659+
} else {
2660+
sessionStorage.removeItem('clerk-token');
2661+
}
2662+
} catch (error) {
2663+
// Session storage might not be available or quota exceeded
2664+
console.warn('Failed to store token in session storage:', error);
2665+
}
2666+
2667+
// Store token in shared worker state
26532668
this.#sharedWorkerManager?.postClerkEvent('clerk:token_update', {
2669+
token: tokenString,
26542670
hasToken: !!payload.token,
26552671
timestamp: Date.now(),
26562672
});
@@ -2704,80 +2720,69 @@ export class Clerk implements ClerkInterface {
27042720
}
27052721

27062722
/**
2707-
* Gets connection information for the current tab's SharedWorker.
2708-
* @public
2709-
*/
2710-
public getSharedWorkerConnectionInfo(): { tabId: string; instanceId: string; isActive: boolean } | null {
2711-
if (this.#sharedWorkerManager) {
2712-
return this.#sharedWorkerManager.getConnectionInfo();
2713-
}
2714-
return null;
2715-
}
2716-
2717-
/**
2718-
* Runs debugging diagnostics on the SharedWorker connection.
2719-
* Useful for troubleshooting SharedWorker issues.
2723+
* Retrieves the stored token from session storage for the current tab.
2724+
* @returns The stored token or null if not found
27202725
* @public
27212726
*/
2722-
public debugSharedWorker(): void {
2723-
if (this.#sharedWorkerManager) {
2724-
this.#sharedWorkerManager.debug();
2725-
} else {
2726-
logger.warnOnce('Clerk: No SharedWorker manager available for debugging');
2727+
public getStoredToken(): string | null {
2728+
try {
2729+
return sessionStorage.getItem('clerk-token');
2730+
} catch (error) {
2731+
console.warn('Failed to retrieve token from session storage:', error);
2732+
return null;
27272733
}
27282734
}
27292735

27302736
/**
2731-
* Sends a message to another tab via the SharedWorker.
2732-
* The receiving tab will console log the message.
2733-
* @param targetTabId - The ID of the tab to send the message to
2734-
* @param message - The message to send
2737+
* Clears the stored token from session storage for the current tab.
27352738
* @public
27362739
*/
2737-
public sendTabMessage(targetTabId: string, message: any): void {
2738-
if (!this.#sharedWorkerManager) {
2739-
logger.warnOnce('Clerk: SharedWorker not initialized. Cannot send tab message.');
2740-
return;
2741-
}
2742-
2743-
if (!this.#sharedWorkerManager.isActive()) {
2744-
logger.warnOnce('Clerk: SharedWorker is not active. Cannot send tab message.');
2745-
return;
2740+
public clearStoredToken(): void {
2741+
try {
2742+
sessionStorage.removeItem('clerk-token');
2743+
} catch (error) {
2744+
console.warn('Failed to clear token from session storage:', error);
27462745
}
2747-
2748-
this.#sharedWorkerManager.sendTabMessage(targetTabId, message);
27492746
}
27502747

27512748
/**
2752-
* Gets the current tab ID for this Clerk instance.
2753-
* @returns The tab ID string or null if SharedWorker is not initialized
2749+
* Retrieves the token from the shared worker state if available.
2750+
* This method sends a request to the shared worker to get the current token.
2751+
* @returns Promise that resolves to the token or null if not available
27542752
* @public
27552753
*/
2756-
public getTabId(): string | null {
2757-
if (!this.#sharedWorkerManager) {
2754+
public async getTokenFromSharedWorker(): Promise<string | null> {
2755+
if (!this.#sharedWorkerManager?.isActive()) {
27582756
return null;
27592757
}
27602758

2761-
return this.#sharedWorkerManager.getTabId();
2762-
}
2759+
try {
2760+
return new Promise((resolve, reject) => {
2761+
const timeout = setTimeout(() => {
2762+
reject(new Error('Timeout waiting for token response'));
2763+
}, 5000);
2764+
2765+
// Set up one-time listener for the response
2766+
const handleMessage = (event: MessageEvent) => {
2767+
if (event.data.type === 'clerk_token_response') {
2768+
clearTimeout(timeout);
2769+
this.#sharedWorkerManager?.getWorker()?.port.removeEventListener('message', handleMessage);
2770+
resolve(event.data.payload.token || null);
2771+
}
2772+
};
27632773

2764-
/**
2765-
* Requests the status of all connected tabs from the SharedWorker.
2766-
* The response will be logged to the console.
2767-
* @public
2768-
*/
2769-
public getConnectedTabs(): void {
2770-
if (!this.#sharedWorkerManager) {
2771-
logger.warnOnce('Clerk: SharedWorker not initialized. Cannot get connected tabs.');
2772-
return;
2773-
}
2774+
this.#sharedWorkerManager?.getWorker()?.port.addEventListener('message', handleMessage);
27742775

2775-
if (!this.#sharedWorkerManager.isActive()) {
2776-
logger.warnOnce('Clerk: SharedWorker is not active. Cannot get connected tabs.');
2777-
return;
2776+
// Request token from shared worker
2777+
this.#sharedWorkerManager?.postMessage({
2778+
type: 'clerk_get_token',
2779+
payload: { timestamp: Date.now() },
2780+
});
2781+
});
2782+
} catch (error) {
2783+
console.warn('Failed to get token from shared worker:', error);
2784+
return null;
27782785
}
2779-
2780-
this.#sharedWorkerManager.getTabStatus();
27812786
}
27822787

27832788
assertComponentsReady(controls: unknown): asserts controls is ReturnType<MountComponentRenderer> {

0 commit comments

Comments
 (0)