@@ -84,6 +84,9 @@ import type {
8484 Web3Provider ,
8585} from '@clerk/types' ;
8686
87+ import type { DebugLoggerInterface } from '@/utils/debug' ;
88+ import { debugLogger , initDebugLogger } from '@/utils/debug' ;
89+
8790import type { MountComponentRenderer } from '../ui/Components' ;
8891import {
8992 ALLOWED_PROTOCOLS ,
@@ -159,6 +162,11 @@ type SetActiveHook = (intent?: 'sign-out') => void | Promise<void>;
159162
160163export type ClerkCoreBroadcastChannelEvent = { type : 'signout' } ;
161164
165+ /**
166+ * Interface for the debug logger with all available logging methods
167+ */
168+ // DebugLoggerInterface imported from '@/utils/debug'
169+
162170declare global {
163171 interface Window {
164172 Clerk ?: Clerk ;
@@ -197,8 +205,8 @@ export class Clerk implements ClerkInterface {
197205 public static sdkMetadata : SDKMetadata = {
198206 name : __PKG_NAME__ ,
199207 version : __PKG_VERSION__ ,
200- environment : process . env . NODE_ENV || 'production' ,
201208 } ;
209+
202210 private static _billing : CommerceBillingNamespace ;
203211 private static _apiKeys : APIKeysNamespace ;
204212 private _checkout : ClerkInterface [ '__experimental_checkout' ] | undefined ;
@@ -210,6 +218,8 @@ export class Clerk implements ClerkInterface {
210218 public __internal_country ?: string | null ;
211219 public telemetry : TelemetryCollector | undefined ;
212220 public readonly __internal_state : State = new State ( ) ;
221+ // Deprecated: use global singleton from `@/utils/debug`
222+ public debugLogger ?: DebugLoggerInterface ;
213223
214224 protected internal_last_error : ClerkAPIError | null = null ;
215225 // converted to protected environment to support `updateEnvironment` type assertion
@@ -404,6 +414,7 @@ export class Clerk implements ClerkInterface {
404414 public getFapiClient = ( ) : FapiClient => this . #fapiClient;
405415
406416 public load = async ( options ?: ClerkOptions ) : Promise < void > => {
417+ debugLogger . info ( 'load() start' , { } , 'clerk' ) ;
407418 if ( this . loaded ) {
408419 return ;
409420 }
@@ -448,10 +459,18 @@ export class Clerk implements ClerkInterface {
448459 } else {
449460 await this . #loadInNonStandardBrowser( ) ;
450461 }
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 ) {
452470 this . #publicEventBus. emit ( clerkEvents . Status , 'error' ) ;
471+ debugLogger . error ( 'load() failed' , { error } , 'clerk' ) ;
453472 // bubble up the error
454- throw e ;
473+ throw error ;
455474 }
456475 } ;
457476
@@ -477,6 +496,16 @@ export class Clerk implements ClerkInterface {
477496 const opts = callbackOrOptions && typeof callbackOrOptions === 'object' ? callbackOrOptions : options || { } ;
478497
479498 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+ ) ;
480509 const signOutCallback = typeof callbackOrOptions === 'function' ? callbackOrOptions : undefined ;
481510
482511 const executeSignOut = async ( ) => {
@@ -520,6 +549,7 @@ export class Clerk implements ClerkInterface {
520549
521550 await executeSignOut ( ) ;
522551
552+ debugLogger . info ( 'signOut() complete' , { redirectUrl : stripOrigin ( redirectUrl ) } , 'clerk' ) ;
523553 return ;
524554 }
525555
@@ -531,6 +561,7 @@ export class Clerk implements ClerkInterface {
531561
532562 if ( shouldSignOutCurrent ) {
533563 await executeSignOut ( ) ;
564+ debugLogger . info ( 'signOut() complete' , { redirectUrl : stripOrigin ( redirectUrl ) } , 'clerk' ) ;
534565 }
535566 } ;
536567
@@ -1202,13 +1233,25 @@ export class Clerk implements ClerkInterface {
12021233 const { organization, beforeEmit, redirectUrl, navigate : setActiveNavigate } = params ;
12031234 let { session } = params ;
12041235 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+ ) ;
12061247 try {
12071248 if ( ! this . client ) {
1249+ debugLogger . warn ( 'Clerk setActive called before client is loaded' , { } , 'clerk' ) ;
12081250 throw new Error ( 'setActive is being called before the client is loaded. Wait for init.' ) ;
12091251 }
12101252
12111253 if ( session === undefined && ! this . session ) {
1254+ debugLogger . warn ( 'Clerk setActive precondition not met: no target session and no active session' , { } , 'clerk' ) ;
12121255 throw new Error (
12131256 'setActive should either be called with a session param or there should be already an active session.' ,
12141257 ) ;
@@ -1414,6 +1457,7 @@ export class Clerk implements ClerkInterface {
14141457 const customNavigate =
14151458 options ?. replace && this . #options. routerReplace ? this . #options. routerReplace : this . #options. routerPush ;
14161459
1460+ debugLogger . info ( `Clerk is navigating to: ${ toURL } ` ) ;
14171461 if ( this . #options. routerDebug ) {
14181462 console . log ( `Clerk is navigating to: ${ toURL } ` ) ;
14191463 }
0 commit comments