@@ -6,6 +6,7 @@ import browser from 'webextension-polyfill';
6
6
import { SCOPE , type Scope } from '../types' ;
7
7
import { CLIENT_JWT_KEY , DEFAULT_LOCAL_HOST_PERMISSION } from './constants' ;
8
8
import { assertPublishableKey } from './utils/errors' ;
9
+ import type { JWTHandlerParams } from './utils/jwt-handler' ;
9
10
import { JWTHandler } from './utils/jwt-handler' ;
10
11
import { validateManifest } from './utils/manifest' ;
11
12
import { requestHandler } from './utils/request-handler' ;
@@ -20,13 +21,15 @@ Clerk.sdkMetadata = {
20
21
} ;
21
22
22
23
export type CreateClerkClientOptions = {
24
+ __experimental_syncHostListener ?: boolean ;
23
25
publishableKey : string ;
24
26
scope ?: Scope ;
25
27
storageCache ?: StorageCache ;
26
28
syncHost ?: string ;
27
29
} ;
28
30
29
31
export async function createClerkClient ( {
32
+ __experimental_syncHostListener = false ,
30
33
publishableKey,
31
34
scope,
32
35
storageCache = BrowserStorageCache ,
@@ -60,17 +63,35 @@ export async function createClerkClient({
60
63
// Set up JWT handler and attempt to get JWT from storage on initialization
61
64
const url = syncHost ? syncHost : DEFAULT_LOCAL_HOST_PERMISSION ;
62
65
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 = {
64
71
frontendApi : key . frontendApi ,
65
72
name : isProd ? CLIENT_JWT_KEY : DEV_BROWSER_JWT_KEY ,
66
- sync,
67
73
url,
74
+ sync : sync ,
68
75
} ;
69
76
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
+
70
87
const jwt = JWTHandler ( storageCache , jwtOptions ) ;
71
88
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
+
74
95
clerk . __unstable__onAfterResponse ( responseHandler ( jwt , { isProd } ) ) ;
75
96
clerk . __unstable__onBeforeRequest ( requestHandler ( jwt , { isProd } ) ) ;
76
97
0 commit comments