Skip to content

Commit c9f379c

Browse files
authored
[Auth] Add option to hide banner in auth when using the emulator (#4112)
* Add option to hide banner in auth when using the emulator * Formatting * Formatting * Add changeset * Update thick-snails-appear.md
1 parent 1b54073 commit c9f379c

File tree

6 files changed

+121
-16
lines changed

6 files changed

+121
-16
lines changed

.changeset/thick-snails-appear.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@firebase/auth": minor
3+
"firebase": minor
4+
---
5+
6+
Add option to hide banner in auth when using the emulator

packages/auth/src/auth.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {
295295
/**
296296
* Sets the emulator configuration (go/firebase-emulator-connection-api).
297297
* @param {string} url The url for the Auth emulator.
298+
* @param {?Object=} options Optional options to specify emulator settings.
298299
*/
299-
fireauth.Auth.prototype.useEmulator = function(url) {
300+
fireauth.Auth.prototype.useEmulator = function(url, options) {
300301
// Emulator config can only be set once.
301302
if (!this.emulatorConfig_) {
302303
if (!/^https?:\/\//.test(url)) {
@@ -305,7 +306,8 @@ fireauth.Auth.prototype.useEmulator = function(url) {
305306
'Emulator URL must start with a valid scheme (http:// or https://).');
306307
}
307308
// Emit a warning so dev knows we are now in test mode.
308-
this.emitEmulatorWarning_();
309+
const disableBanner = options ? !!options['disableWarnings'] : false;
310+
this.emitEmulatorWarning_(disableBanner);
309311
// Persist the config.
310312
this.emulatorConfig_ = { url };
311313
// Disable app verification.
@@ -319,14 +321,16 @@ fireauth.Auth.prototype.useEmulator = function(url) {
319321

320322

321323
/**
322-
* Emits a console warning and a visual banner if emulator integration is
324+
* Emits a console info and a visual banner if emulator integration is
323325
* enabled.
326+
* @param {boolean} disableBanner Whether visual banner should be disabled.
327+
* @private
324328
*/
325-
fireauth.Auth.prototype.emitEmulatorWarning_ = function() {
326-
fireauth.util.consoleWarn('WARNING: You are using the Auth Emulator,' +
329+
fireauth.Auth.prototype.emitEmulatorWarning_ = function(disableBanner) {
330+
fireauth.util.consoleInfo('WARNING: You are using the Auth Emulator,' +
327331
' which is intended for local testing only. Do not use with' +
328332
' production credentials.');
329-
if (goog.global.document) {
333+
if (goog.global.document && !disableBanner) {
330334
fireauth.util.onDomReady().then(() => {
331335
const ele = goog.global.document.createElement('div');
332336
ele.innerText = 'Running in emulator mode. Do not use with production' +

packages/auth/src/exports_auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ fireauth.exportlib.exportPrototypeMethods(
200200
useEmulator: {
201201
name: 'useEmulator',
202202
args: [
203-
fireauth.args.string('url')
203+
fireauth.args.string('url'),
204+
fireauth.args.object('options', true)
204205
]
205206
},
206207
verifyPasswordResetCode: {

packages/auth/src/utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -1406,6 +1406,17 @@ fireauth.util.consoleWarn = function(message) {
14061406
};
14071407

14081408

1409+
/**
1410+
* Logs an info message to the console, if the console is available.
1411+
* @param {string} message
1412+
*/
1413+
fireauth.util.consoleInfo = function(message) {
1414+
if (typeof console !== 'undefined' && typeof console.info === 'function') {
1415+
console.info(message);
1416+
}
1417+
};
1418+
1419+
14091420
/**
14101421
* Parses a UTC time stamp string or number and returns the corresponding UTC
14111422
* date string if valid. Otherwise, returns null.

packages/auth/test/auth_test.js

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ function testUseEmulator() {
914914
goog.testing.recordFunction());
915915
stubs.replace(
916916
fireauth.util,
917-
'consoleWarn',
917+
'consoleInfo',
918918
goog.testing.recordFunction());
919919
var handler = goog.testing.recordFunction();
920920
stubs.replace(
@@ -935,7 +935,7 @@ function testUseEmulator() {
935935
assertEquals(0, handler.getCallCount());
936936
assertEquals(
937937
0, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
938-
assertEquals(0, fireauth.util.consoleWarn.getCallCount());
938+
assertEquals(0, fireauth.util.consoleInfo.getCallCount());
939939
assertEquals(
940940
0,
941941
fireauth.AuthSettings.prototype.setAppVerificationDisabledForTesting.
@@ -958,8 +958,17 @@ function testUseEmulator() {
958958
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
959959
.getArgument(0)
960960
);
961-
// Should emit a console warning.
962-
assertEquals(1, fireauth.util.consoleWarn.getCallCount());
961+
// Should emit a console warning and a banner.
962+
assertEquals(1, fireauth.util.consoleInfo.getCallCount());
963+
if (goog.global.document) {
964+
asyncTestCase.waitForSignals(1);
965+
fireauth.util.onDomReady().then(() => {
966+
const el =
967+
goog.global.document.querySelector('.firebase-emulator-warning');
968+
assertNotNull(el);
969+
asyncTestCase.signal();
970+
});
971+
}
963972
// Should disable App verification.
964973
assertEquals(
965974
true,
@@ -975,7 +984,7 @@ function testUseEmulator() {
975984
auth1.getEmulatorConfig());
976985
assertEquals(
977986
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
978-
assertEquals(1, fireauth.util.consoleWarn.getCallCount());
987+
assertEquals(1, fireauth.util.consoleInfo.getCallCount());
979988

980989
// Updating to different config should still not trigger event.
981990
auth1.useEmulator('http://emulator.other.domain:9876');
@@ -989,6 +998,58 @@ function testUseEmulator() {
989998
}
990999

9911000

1001+
function testUseEmulator_withDisableWarnings() {
1002+
// Listen to emulator config calls on RpcHandler.
1003+
stubs.replace(
1004+
fireauth.RpcHandler.prototype, 'updateEmulatorConfig',
1005+
goog.testing.recordFunction());
1006+
stubs.replace(fireauth.util, 'consoleInfo', goog.testing.recordFunction());
1007+
const handler = goog.testing.recordFunction();
1008+
1009+
app1 = firebase.initializeApp(config1, appId1);
1010+
auth1 = app1.auth();
1011+
1012+
// Listen to all emulatorConfigChange events dispatched by the Auth instance.
1013+
goog.events.listen(
1014+
auth1, fireauth.constants.AuthEventType.EMULATOR_CONFIG_CHANGED, handler);
1015+
1016+
assertUndefined(fireauth.constants.emulatorConfig);
1017+
assertEquals(0, handler.getCallCount());
1018+
assertEquals(
1019+
0, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
1020+
assertEquals(0, fireauth.util.consoleInfo.getCallCount());
1021+
1022+
// Update the emulator config.
1023+
auth1.useEmulator(
1024+
'http://emulator.test.domain:1234', {disableWarnings: true});
1025+
assertObjectEquals(
1026+
{
1027+
url: 'http://emulator.test.domain:1234',
1028+
},
1029+
auth1.getEmulatorConfig());
1030+
// Should notify the RPC handler.
1031+
assertEquals(
1032+
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
1033+
assertObjectEquals(
1034+
{
1035+
url: 'http://emulator.test.domain:1234',
1036+
},
1037+
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
1038+
.getArgument(0));
1039+
// Should emit a console info but not a banner.
1040+
assertEquals(1, fireauth.util.consoleInfo.getCallCount());
1041+
if (goog.global.document) {
1042+
asyncTestCase.waitForSignals(1);
1043+
fireauth.util.onDomReady().then(() => {
1044+
const el =
1045+
goog.global.document.querySelector('.firebase-emulator-warning');
1046+
assertNull(el);
1047+
asyncTestCase.signal();
1048+
});
1049+
}
1050+
}
1051+
1052+
9921053
function testGetSetTenantId() {
9931054
app1 = firebase.initializeApp(config1, appId1);
9941055
auth1 = app1.auth();

packages/auth/test/utils_test.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -1791,8 +1791,8 @@ function testConsoleWarn() {
17911791
// those browsers.
17921792
return;
17931793
}
1794-
var consoleWarn = mockControl.createMethodMock(goog.global.console, 'warn');
1795-
var message = 'This is my message.';
1794+
const consoleWarn = mockControl.createMethodMock(goog.global.console, 'warn');
1795+
const message = 'This is my message.';
17961796
consoleWarn(message).$once();
17971797

17981798
mockControl.$replayAll();
@@ -1806,6 +1806,28 @@ function testConsoleWarn_doesntBreakIE() {
18061806
}
18071807

18081808

1809+
function testConsoleInfo() {
1810+
if (typeof console === 'undefined') {
1811+
// Ignore browsers that don't support console. The test
1812+
// testConsoleInfo_doesntBreakIE tests that this function doesn't break
1813+
// those browsers.
1814+
return;
1815+
}
1816+
const consoleInfo = mockControl.createMethodMock(goog.global.console, 'info');
1817+
const message = 'This is my message.';
1818+
consoleInfo(message).$once();
1819+
1820+
mockControl.$replayAll();
1821+
1822+
fireauth.util.consoleInfo(message);
1823+
}
1824+
1825+
1826+
function testConsoleInfo_doesntBreakIE() {
1827+
fireauth.util.consoleInfo('This should not trigger an error in IE.');
1828+
}
1829+
1830+
18091831
function testUtcTimestampToDateString() {
18101832
var actual;
18111833
// Null.

0 commit comments

Comments
 (0)