Skip to content

Commit 675fb02

Browse files
committed
Fix debug initialize flag
1 parent 0c329f7 commit 675fb02

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import * as client from './client';
3939
import * as storage from './storage';
4040
import * as internalApi from './internal-api';
4141
import * as indexeddb from './indexeddb';
42+
import * as debug from './debug';
4243
import { deleteApp, FirebaseApp } from '@firebase/app';
4344
import { CustomProvider, ReCaptchaV3Provider } from './providers';
4445
import { AppCheckService } from './factory';
@@ -140,18 +141,27 @@ describe('api', () => {
140141
expect(consoleStub.args[0][0]).to.include(token);
141142
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
142143
});
143-
it('warns about debug mode on second call', async () => {
144+
it('does not call initializeDebugMode on second call', async () => {
144145
self.FIREBASE_APPCHECK_DEBUG_TOKEN = 'abcdefg';
145146
const consoleStub = stub(console, 'log');
147+
const initializeDebugModeSpy = spy(debug, 'initializeDebugMode');
148+
// First call, should call initializeDebugMode()
146149
initializeAppCheck(app, {
147150
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
148151
});
152+
expect(initializeDebugModeSpy).to.be.called;
153+
initializeDebugModeSpy.resetHistory();
154+
// Second call, should not call initializeDebugMode()
149155
initializeAppCheck(app, {
150156
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
151157
});
152158
const token = await getDebugToken();
153159
expect(token).to.equal('abcdefg');
160+
// Two console logs of the token, for each initializeAppCheck call.
154161
expect(consoleStub.args[0][0]).to.include(token);
162+
expect(consoleStub.args[1][0]).to.include(token);
163+
expect(consoleStub.args[1][0]).to.equal(consoleStub.args[0][0]);
164+
expect(initializeDebugModeSpy).to.not.be.called;
155165
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
156166
});
157167

packages/app-check/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export function initializeAppCheck(
6363
initializeDebugMode();
6464
}
6565

66-
// Log a warning when `initializeAppCheck()` is called in debug mode,
67-
// and show the token.
66+
// Log a message containing the debug token when `initializeAppCheck()`
67+
// is called in debug mode.
6868
if (isDebugMode()) {
6969
// Do not block initialization to get the token for the message.
7070
void getDebugToken().then(token =>

packages/app-check/src/debug.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ export async function getDebugToken(): Promise<string> {
4646

4747
export function initializeDebugMode(): void {
4848
const globals = getGlobal();
49+
const debugState = getDebugState();
50+
// Set to true if this function has been called, whether or not
51+
// it enabled debug mode.
52+
debugState.initialized = true;
53+
4954
if (
5055
typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
5156
globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== true
5257
) {
5358
return;
5459
}
5560

56-
const debugState = getDebugState();
5761
debugState.enabled = true;
5862
const deferredToken = new Deferred<string>();
5963
debugState.token = deferredToken;

packages/app-check/src/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function clearState(): void {
7070
APP_CHECK_STATES.clear();
7171
DEBUG_STATE.enabled = false;
7272
DEBUG_STATE.token = undefined;
73+
DEBUG_STATE.initialized = false;
7374
}
7475

7576
export function getDebugState(): DebugState {

0 commit comments

Comments
 (0)