@@ -6,6 +6,7 @@ import browser from 'webextension-polyfill';
66import { SCOPE , type Scope } from '../types' ;
77import { CLIENT_JWT_KEY , DEFAULT_LOCAL_HOST_PERMISSION } from './constants' ;
88import { assertPublishableKey } from './utils/errors' ;
9+ import type { JWTHandlerParams } from './utils/jwt-handler' ;
910import { JWTHandler } from './utils/jwt-handler' ;
1011import { validateManifest } from './utils/manifest' ;
1112import { requestHandler } from './utils/request-handler' ;
@@ -20,13 +21,15 @@ Clerk.sdkMetadata = {
2021} ;
2122
2223export type CreateClerkClientOptions = {
24+ __experimental_syncHostListener ?: boolean ;
2325 publishableKey : string ;
2426 scope ?: Scope ;
2527 storageCache ?: StorageCache ;
2628 syncHost ?: string ;
2729} ;
2830
2931export async function createClerkClient ( {
32+ __experimental_syncHostListener = false ,
3033 publishableKey,
3134 scope,
3235 storageCache = BrowserStorageCache ,
@@ -60,17 +63,35 @@ export async function createClerkClient({
6063 // Set up JWT handler and attempt to get JWT from storage on initialization
6164 const url = syncHost ? syncHost : DEFAULT_LOCAL_HOST_PERMISSION ;
6265
63- const jwtOptions = {
66+ // Create Clerk instance
67+ clerk = new Clerk ( publishableKey ) ;
68+
69+ // @ts -expect-error - TODO: sync is evaluating to true vs boolean
70+ const jwtOptions : JWTHandlerParams = {
6471 frontendApi : key . frontendApi ,
6572 name : isProd ? CLIENT_JWT_KEY : DEV_BROWSER_JWT_KEY ,
66- sync,
6773 url,
74+ sync : sync ,
6875 } ;
6976
77+ if ( jwtOptions . sync && __experimental_syncHostListener ) {
78+ jwtOptions . onListenerCallback = ( ) => {
79+ if ( clerk . user ) {
80+ clerk . user . reload ( ) ;
81+ } else {
82+ window . location . reload ( ) ;
83+ }
84+ } ;
85+ }
86+
7087 const jwt = JWTHandler ( storageCache , jwtOptions ) ;
7188
72- // Create Clerk instance
73- clerk = new Clerk ( publishableKey ) ;
89+ // Add listener to sync host cookies if enabled
90+ if ( jwtOptions . sync && __experimental_syncHostListener ) {
91+ const listener = jwt . listener ( ) ;
92+ listener ?. add ( ) ;
93+ }
94+
7495 clerk . __unstable__onAfterResponse ( responseHandler ( jwt , { isProd } ) ) ;
7596 clerk . __unstable__onBeforeRequest ( requestHandler ( jwt , { isProd } ) ) ;
7697
0 commit comments