Skip to content

Commit bb65d49

Browse files
authored
fix: add emulator support for auth (#805)
* Add Auth emulator support
1 parent 9ab63a2 commit bb65d49

File tree

5 files changed

+183
-128
lines changed

5 files changed

+183
-128
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* Adds Emulator support to Auth UI. Fixes
2+
https://github.com/firebase/firebaseui-web/issues/778

javascript/testing/auth.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ goog.module('firebaseui.auth.testing.FakeAuthClient');
2121
goog.module.declareLegacyNamespace();
2222
goog.setTestOnly();
2323

24-
var MockHelper = goog.require('firebaseui.auth.testing.MockHelper');
25-
var array = goog.require('goog.array');
24+
const MockHelper = goog.require('firebaseui.auth.testing.MockHelper');
25+
const Uri = goog.require('goog.Uri');
26+
const array = goog.require('goog.array');
27+
2628

2729
/**
2830
* Fake Auth API client class.
@@ -37,6 +39,7 @@ class FakeAuthClient extends MockHelper {
3739
this.user_ = {};
3840
this['app'] = app;
3941
this['currentUser'] = null;
42+
this['emulatorConfig'] = null;
4043
var asyncMethods = {};
4144
// Populate auth async methods.
4245
for (var key in FakeAuthClient.AuthAsyncMethod) {
@@ -66,6 +69,23 @@ class FakeAuthClient extends MockHelper {
6669
};
6770
}
6871

72+
/**
73+
* Mock useEmulator() function from main SDK.
74+
* @param {string} url The url to talk to the emulator with.
75+
* @param {?Object} options Options for emulator config.
76+
*/
77+
useEmulator(url, options) {
78+
const uri = Uri.parse(url);
79+
this['emulatorConfig'] = {
80+
'protocol': uri.getScheme(),
81+
'host': uri.getDomain(),
82+
'port': uri.getPort(),
83+
'options': {
84+
'disableWarnings': options ? options.disableWarnings : false,
85+
},
86+
};
87+
}
88+
6989
/**
7090
* Asserts the expected list of frameworks IDs are logged.
7191
* @param {!Array<string>} expectedFrameworks The expected list of frameworks

javascript/widgets/authui.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ firebaseui.auth.AuthUI = function(auth, opt_appId) {
129129
* instance.
130130
*/
131131
this.tempAuth_ = tempApp.auth();
132+
const emulatorConfig = auth.emulatorConfig;
133+
if (emulatorConfig) {
134+
const {protocol, host, port, options} = emulatorConfig;
135+
const portString = port === null ? '' : ':' + port;
136+
this.tempAuth_.useEmulator(`${protocol}://${host}${portString}`, options);
137+
}
132138
// Log FirebaseUI on internal Auth instance.
133139
firebaseui.auth.AuthUI.logFirebaseUI_(this.tempAuth_);
134140
// Change persistence to session to avoid the risk of dangling auth states in

javascript/widgets/authui_test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ function testGetExternalAuth() {
516516
assertEquals('testapp2-firebaseui-temp', app2.getAuth().app.name);
517517
}
518518

519+
519520
function testTempAuth_sessionPersistence() {
520521
createAndInstallTestInstances();
521522
// Initialize app.
@@ -528,6 +529,47 @@ function testTempAuth_sessionPersistence() {
528529
}
529530

530531

532+
function testTempAuth_emulatorConfig() {
533+
createAndInstallTestInstances();
534+
// Initialize app.
535+
testAuth.install();
536+
testAuth.useEmulator('http://localhost:1234');
537+
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
538+
// Confirm correct name used for temp instance.
539+
assertEquals('testapp1-firebaseui-temp', app1.getAuth().app.name);
540+
// Confirm emulator config properly set on internal instance.
541+
assertObjectEquals({
542+
protocol: 'http',
543+
host: 'localhost',
544+
port: 1234,
545+
options: {
546+
disableWarnings: false,
547+
}
548+
}, app.getAuth().emulatorConfig);
549+
}
550+
551+
552+
function testTempAuth_emulatorConfig_handlesIPV6Hosts() {
553+
createAndInstallTestInstances();
554+
// Initialize app.
555+
testAuth.install();
556+
testAuth.useEmulator(
557+
'http://[0:0:0:0:0:0:0:0]:1234', {disableWarnings: true});
558+
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
559+
// Confirm correct name used for temp instance.
560+
assertEquals('testapp1-firebaseui-temp', app1.getAuth().app.name);
561+
// Confirm emulator config hasn't double-quoted IPv6 address.
562+
assertObjectEquals({
563+
protocol: 'http',
564+
host: '[0:0:0:0:0:0:0:0]',
565+
port: 1234,
566+
options: {
567+
disableWarnings: true,
568+
}
569+
}, app.getAuth().emulatorConfig);
570+
}
571+
572+
531573
function testAppId() {
532574
createAndInstallTestInstances();
533575
// Confirm correct app id stored for each app.

0 commit comments

Comments
 (0)