@@ -23,7 +23,7 @@ import {
23
23
PartialObserver
24
24
} from './public-types' ;
25
25
import { ERROR_FACTORY , AppCheckError } from './errors' ;
26
- import { getState , setState , AppCheckState } from './state' ;
26
+ import { getState , setState , AppCheckState , getDebugState } from './state' ;
27
27
import { FirebaseApp , getApp , _getProvider } from '@firebase/app' ;
28
28
import { getModularInstance , ErrorFn , NextFn } from '@firebase/util' ;
29
29
import { AppCheckService } from './factory' ;
@@ -59,6 +59,26 @@ export function initializeAppCheck(
59
59
app = getModularInstance ( app ) ;
60
60
const provider = _getProvider ( app , 'app-check' ) ;
61
61
62
+ // Ensure initializeDebugMode() is only called once.
63
+ if ( ! getDebugState ( ) . initialized ) {
64
+ initializeDebugMode ( ) ;
65
+ }
66
+
67
+ // Log a warning when `initializeAppCheck()` is called in debug mode,
68
+ // and show the token.
69
+ if ( isDebugMode ( ) ) {
70
+ logger . warn (
71
+ `App Check is in debug mode. To turn off debug mode, unset ` +
72
+ `the global variable FIREBASE_APPCHECK_DEBUG_TOKEN and ` +
73
+ `restart the app.`
74
+ ) ;
75
+ // Make this a separate console statement so user will at least have the
76
+ // first message if the token promise doesn't resolve in time.
77
+ void getDebugToken ( ) . then ( token =>
78
+ logger . warn ( `Debug token is ${ token } .` )
79
+ ) ;
80
+ }
81
+
62
82
if ( provider . isInitialized ( ) ) {
63
83
const existingInstance = provider . getImmediate ( ) ;
64
84
const initialOptions = provider . getOptions ( ) as unknown as AppCheckOptions ;
@@ -67,21 +87,6 @@ export function initializeAppCheck(
67
87
options . isTokenAutoRefreshEnabled &&
68
88
initialOptions . provider . isEqual ( options . provider )
69
89
) {
70
- // Log a warning if `initializeAppCheck()` is called after the first time
71
- // if this app is still in debug mode, and show the token.
72
- // If it's the first time, `initializeDebugMode()` already logs a message.
73
- if ( isDebugMode ( ) ) {
74
- logger . warn (
75
- `App Check is in debug mode. To turn off debug mode, unset ` +
76
- `the global variable FIREBASE_APPCHECK_DEBUG_TOKEN and ` +
77
- `restart the app.`
78
- ) ;
79
- // Make this a separate console statement so user will at least have the
80
- // first message if the token promise doesn't resolve in time.
81
- void getDebugToken ( ) . then ( token =>
82
- logger . warn ( `Debug token is ${ token } .` )
83
- ) ;
84
- }
85
90
return existingInstance ;
86
91
} else {
87
92
throw ERROR_FACTORY . create ( AppCheckError . ALREADY_INITIALIZED , {
@@ -90,9 +95,6 @@ export function initializeAppCheck(
90
95
}
91
96
}
92
97
93
- // Only read global variable on first call to `initializeAppCheck()`.
94
- initializeDebugMode ( ) ;
95
-
96
98
const appCheck = provider . initialize ( { options } ) ;
97
99
_activate ( app , options . provider , options . isTokenAutoRefreshEnabled ) ;
98
100
0 commit comments