Skip to content

Commit c5a90de

Browse files
committed
Address PR comments
1 parent 86331a5 commit c5a90de

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

packages/app-check/src/api.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,29 @@ describe('api', () => {
121121
).to.equal(appCheckInstance);
122122
});
123123
it('starts debug mode on first call', async () => {
124-
const fakeWrite = (): Promise<void> => Promise.resolve();
125-
const writeToIndexedDBStub = stub(
124+
let token: string = '';
125+
const fakeWrite = (tokenToWrite: string): Promise<void> => {
126+
token = tokenToWrite;
127+
return Promise.resolve();
128+
};
129+
stub(
126130
indexeddb,
127131
'writeDebugTokenToIndexedDB'
128132
).callsFake(fakeWrite);
129-
stub(indexeddb, 'readDebugTokenFromIndexedDB').callsFake(() =>
130-
Promise.resolve(undefined)
131-
);
133+
stub(indexeddb, 'readDebugTokenFromIndexedDB').resolves(token);
132134
const logStub = stub(logger.logger, 'warn');
133135
const consoleStub = stub(console, 'log');
134136
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
135137
initializeAppCheck(app, {
136138
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
137139
});
138-
await fakeWrite();
139-
const token = writeToIndexedDBStub.args[0];
140-
expect(logStub).to.not.be.called;
140+
// Ensure getDebugToken() call inside `initializeAppCheck()`
141+
// has time to resolve, and double check its value matches that
142+
// written to indexedDB.
143+
expect(await getDebugToken()).to.equal(token);
141144
expect(consoleStub.args[0][0]).to.include(token);
145+
expect(logStub).to.be.calledWith(match('is in debug mode'));
146+
expect(logStub).to.be.calledWith(match(token));
142147
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
143148
});
144149
it('warns about debug mode on second call', async () => {

packages/app-check/src/api.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
PartialObserver
2424
} from './public-types';
2525
import { ERROR_FACTORY, AppCheckError } from './errors';
26-
import { getState, setState, AppCheckState } from './state';
26+
import { getState, setState, AppCheckState, getDebugState } from './state';
2727
import { FirebaseApp, getApp, _getProvider } from '@firebase/app';
2828
import { getModularInstance, ErrorFn, NextFn } from '@firebase/util';
2929
import { AppCheckService } from './factory';
@@ -59,6 +59,26 @@ export function initializeAppCheck(
5959
app = getModularInstance(app);
6060
const provider = _getProvider(app, 'app-check');
6161

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+
6282
if (provider.isInitialized()) {
6383
const existingInstance = provider.getImmediate();
6484
const initialOptions = provider.getOptions() as unknown as AppCheckOptions;
@@ -67,21 +87,6 @@ export function initializeAppCheck(
6787
options.isTokenAutoRefreshEnabled &&
6888
initialOptions.provider.isEqual(options.provider)
6989
) {
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-
}
8590
return existingInstance;
8691
} else {
8792
throw ERROR_FACTORY.create(AppCheckError.ALREADY_INITIALIZED, {
@@ -90,9 +95,6 @@ export function initializeAppCheck(
9095
}
9196
}
9297

93-
// Only read global variable on first call to `initializeAppCheck()`.
94-
initializeDebugMode();
95-
9698
const appCheck = provider.initialize({ options });
9799
_activate(app, options.provider, options.isTokenAutoRefreshEnabled);
98100

packages/app-check/src/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ReCAPTCHAState {
4141
}
4242

4343
export interface DebugState {
44+
initialized: boolean;
4445
enabled: boolean;
4546
token?: Deferred<string>;
4647
}
@@ -52,6 +53,7 @@ export const DEFAULT_STATE: AppCheckState = {
5253
};
5354

5455
const DEBUG_STATE: DebugState = {
56+
initialized: false,
5557
enabled: false
5658
};
5759

0 commit comments

Comments
 (0)