@@ -84,6 +84,9 @@ import type {
84
84
Web3Provider ,
85
85
} from '@clerk/types' ;
86
86
87
+ import type { DebugLoggerInterface } from '@/utils/debug' ;
88
+ import { debugLogger , initDebugLogger } from '@/utils/debug' ;
89
+
87
90
import type { MountComponentRenderer } from '../ui/Components' ;
88
91
import {
89
92
ALLOWED_PROTOCOLS ,
@@ -159,6 +162,11 @@ type SetActiveHook = (intent?: 'sign-out') => void | Promise<void>;
159
162
160
163
export type ClerkCoreBroadcastChannelEvent = { type : 'signout' } ;
161
164
165
+ /**
166
+ * Interface for the debug logger with all available logging methods
167
+ */
168
+ // DebugLoggerInterface imported from '@/utils/debug'
169
+
162
170
declare global {
163
171
interface Window {
164
172
Clerk ?: Clerk ;
@@ -197,8 +205,8 @@ export class Clerk implements ClerkInterface {
197
205
public static sdkMetadata : SDKMetadata = {
198
206
name : __PKG_NAME__ ,
199
207
version : __PKG_VERSION__ ,
200
- environment : process . env . NODE_ENV || 'production' ,
201
208
} ;
209
+
202
210
private static _billing : CommerceBillingNamespace ;
203
211
private static _apiKeys : APIKeysNamespace ;
204
212
private _checkout : ClerkInterface [ '__experimental_checkout' ] | undefined ;
@@ -210,6 +218,8 @@ export class Clerk implements ClerkInterface {
210
218
public __internal_country ?: string | null ;
211
219
public telemetry : TelemetryCollector | undefined ;
212
220
public readonly __internal_state : State = new State ( ) ;
221
+ // Deprecated: use global singleton from `@/utils/debug`
222
+ public debugLogger ?: DebugLoggerInterface ;
213
223
214
224
protected internal_last_error : ClerkAPIError | null = null ;
215
225
// converted to protected environment to support `updateEnvironment` type assertion
@@ -404,6 +414,7 @@ export class Clerk implements ClerkInterface {
404
414
public getFapiClient = ( ) : FapiClient => this . #fapiClient;
405
415
406
416
public load = async ( options ?: ClerkOptions ) : Promise < void > => {
417
+ debugLogger . info ( 'load() start' , { } , 'clerk' ) ;
407
418
if ( this . loaded ) {
408
419
return ;
409
420
}
@@ -448,10 +459,18 @@ export class Clerk implements ClerkInterface {
448
459
} else {
449
460
await this . #loadInNonStandardBrowser( ) ;
450
461
}
451
- } catch ( e ) {
462
+ if ( this . environment ?. clientDebugMode ) {
463
+ initDebugLogger ( {
464
+ enabled : true ,
465
+ telemetryCollector : this . telemetry ,
466
+ } ) ;
467
+ }
468
+ debugLogger . info ( 'load() complete' , { } , 'clerk' ) ;
469
+ } catch ( error ) {
452
470
this . #publicEventBus. emit ( clerkEvents . Status , 'error' ) ;
471
+ debugLogger . error ( 'load() failed' , { error } , 'clerk' ) ;
453
472
// bubble up the error
454
- throw e ;
473
+ throw error ;
455
474
}
456
475
} ;
457
476
@@ -477,6 +496,16 @@ export class Clerk implements ClerkInterface {
477
496
const opts = callbackOrOptions && typeof callbackOrOptions === 'object' ? callbackOrOptions : options || { } ;
478
497
479
498
const redirectUrl = opts ?. redirectUrl || this . buildAfterSignOutUrl ( ) ;
499
+ debugLogger . debug (
500
+ 'signOut() start' ,
501
+ {
502
+ hasClient : Boolean ( this . client ) ,
503
+ multiSessionCount : this . client ?. signedInSessions . length ?? 0 ,
504
+ redirectUrl,
505
+ sessionTarget : opts ?. sessionId ?? null ,
506
+ } ,
507
+ 'clerk' ,
508
+ ) ;
480
509
const signOutCallback = typeof callbackOrOptions === 'function' ? callbackOrOptions : undefined ;
481
510
482
511
const executeSignOut = async ( ) => {
@@ -520,6 +549,7 @@ export class Clerk implements ClerkInterface {
520
549
521
550
await executeSignOut ( ) ;
522
551
552
+ debugLogger . info ( 'signOut() complete' , { redirectUrl : stripOrigin ( redirectUrl ) } , 'clerk' ) ;
523
553
return ;
524
554
}
525
555
@@ -531,6 +561,7 @@ export class Clerk implements ClerkInterface {
531
561
532
562
if ( shouldSignOutCurrent ) {
533
563
await executeSignOut ( ) ;
564
+ debugLogger . info ( 'signOut() complete' , { redirectUrl : stripOrigin ( redirectUrl ) } , 'clerk' ) ;
534
565
}
535
566
} ;
536
567
@@ -1202,13 +1233,25 @@ export class Clerk implements ClerkInterface {
1202
1233
const { organization, beforeEmit, redirectUrl, navigate : setActiveNavigate } = params ;
1203
1234
let { session } = params ;
1204
1235
this . __internal_setActiveInProgress = true ;
1205
-
1236
+ debugLogger . debug (
1237
+ 'setActive() start' ,
1238
+ {
1239
+ hasClient : Boolean ( this . client ) ,
1240
+ sessionTarget : typeof session === 'string' ? session : ( session ?. id ?? session ?? null ) ,
1241
+ organizationTarget :
1242
+ typeof organization === 'string' ? organization : ( organization ?. id ?? organization ?? null ) ,
1243
+ redirectUrl : redirectUrl ?? null ,
1244
+ } ,
1245
+ 'clerk' ,
1246
+ ) ;
1206
1247
try {
1207
1248
if ( ! this . client ) {
1249
+ debugLogger . warn ( 'Clerk setActive called before client is loaded' , { } , 'clerk' ) ;
1208
1250
throw new Error ( 'setActive is being called before the client is loaded. Wait for init.' ) ;
1209
1251
}
1210
1252
1211
1253
if ( session === undefined && ! this . session ) {
1254
+ debugLogger . warn ( 'Clerk setActive precondition not met: no target session and no active session' , { } , 'clerk' ) ;
1212
1255
throw new Error (
1213
1256
'setActive should either be called with a session param or there should be already an active session.' ,
1214
1257
) ;
@@ -1414,6 +1457,7 @@ export class Clerk implements ClerkInterface {
1414
1457
const customNavigate =
1415
1458
options ?. replace && this . #options. routerReplace ? this . #options. routerReplace : this . #options. routerPush ;
1416
1459
1460
+ debugLogger . info ( `Clerk is navigating to: ${ toURL } ` ) ;
1417
1461
if ( this . #options. routerDebug ) {
1418
1462
console . log ( `Clerk is navigating to: ${ toURL } ` ) ;
1419
1463
}
0 commit comments