@@ -184,6 +184,12 @@ fireauth.Auth = function(app) {
184
184
* is currently only used to log FirebaseUI.
185
185
*/
186
186
this . frameworks_ = [ ] ;
187
+
188
+ /**
189
+ * @private {?fireauth.constants.EmulatorSettings} The current
190
+ * emulator settings.
191
+ */
192
+ this . emulatorConfig_ = null ;
187
193
} ;
188
194
goog . inherits ( fireauth . Auth , goog . events . EventTarget ) ;
189
195
@@ -202,6 +208,20 @@ fireauth.Auth.LanguageCodeChangeEvent = function(languageCode) {
202
208
goog . inherits ( fireauth . Auth . LanguageCodeChangeEvent , goog . events . Event ) ;
203
209
204
210
211
+ /**
212
+ * Emulator config change custom event.
213
+ * @param {?fireauth.constants.EmulatorSettings } emulatorConfig The new
214
+ * emulator settings.
215
+ * @constructor
216
+ * @extends {goog.events.Event }
217
+ */
218
+ fireauth . Auth . EmulatorConfigChangeEvent = function ( emulatorConfig ) {
219
+ goog . events . Event . call ( this , fireauth . constants . AuthEventType . EMULATOR_CONFIG_CHANGED ) ;
220
+ this . emulatorConfig = emulatorConfig ;
221
+ } ;
222
+ goog . inherits ( fireauth . Auth . EmulatorConfigChangeEvent , goog . events . Event ) ;
223
+
224
+
205
225
/**
206
226
* Framework change custom event.
207
227
* @param {!Array<string> } frameworks The new frameworks array.
@@ -272,6 +292,65 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {
272
292
} ;
273
293
274
294
295
+ /**
296
+ * Sets the emulator configuration (go/firebase-emulator-connection-api).
297
+ * @param {string } url The url for the Auth emulator.
298
+ */
299
+ fireauth . Auth . prototype . useEmulator = function ( url ) {
300
+ // Emulator config can only be set once.
301
+ if ( ! this . emulatorConfig_ ) {
302
+ // Emit a warning so dev knows we are now in test mode.
303
+ this . emitEmulatorWarning_ ( ) ;
304
+ // Persist the config.
305
+ this . emulatorConfig_ = { url } ;
306
+ // Disable app verification.
307
+ this . settings_ ( ) . setAppVerificationDisabledForTesting ( true ) ;
308
+ // Update RPC handler endpoints.
309
+ this . rpcHandler_ . updateEmulatorConfig ( this . emulatorConfig_ ) ;
310
+ // Notify external event listeners.
311
+ this . notifyEmulatorConfigListeners_ ( ) ;
312
+ }
313
+ }
314
+
315
+
316
+ /**
317
+ * Emits a console warning and a visual banner if emulator integration is
318
+ * enabled.
319
+ */
320
+ fireauth . Auth . prototype . emitEmulatorWarning_ = function ( ) {
321
+ fireauth . util . consoleWarn ( 'WARNING: You are using the Auth Emulator,' +
322
+ ' which is intended for local testing only. Do not use with' +
323
+ ' production credentials.' ) ;
324
+ if ( goog . global . document ) {
325
+ fireauth . util . onDomReady ( ) . then ( ( ) => {
326
+ const ele = goog . global . document . createElement ( 'div' ) ;
327
+ ele . innerText = 'Running in emulator mode. Do not use with production' +
328
+ ' credentials.' ;
329
+ ele . style . position = 'fixed' ;
330
+ ele . style . width = '100%' ;
331
+ ele . style . backgroundColor = '#ffffff' ;
332
+ ele . style . border = '.1em solid #000000' ;
333
+ ele . style . color = '#ff0000' ;
334
+ ele . style . bottom = '0px' ;
335
+ ele . style . left = '0px' ;
336
+ ele . style . margin = '0px' ;
337
+ ele . style . zIndex = 10000 ;
338
+ ele . style . textAlign = 'center' ;
339
+ ele . classList . add ( 'firebase-emulator-warning' ) ;
340
+ goog . global . document . body . appendChild ( ele ) ;
341
+ } ) ;
342
+ }
343
+ }
344
+
345
+
346
+ /**
347
+ * @return {?fireauth.constants.EmulatorSettings }
348
+ */
349
+ fireauth . Auth . prototype . getEmulatorConfig = function ( ) {
350
+ return this . emulatorConfig_ ;
351
+ }
352
+
353
+
275
354
/**
276
355
* @param {string } frameworkId The framework identifier.
277
356
*/
@@ -396,7 +475,15 @@ fireauth.Auth.prototype.notifyLanguageCodeListeners_ = function() {
396
475
} ;
397
476
398
477
399
-
478
+ /**
479
+ * Notifies all external listeners of the emulator config change.
480
+ * @private
481
+ */
482
+ fireauth . Auth . prototype . notifyEmulatorConfigListeners_ = function ( ) {
483
+ // Notify external listeners on the emulator config change.
484
+ this . dispatchEvent (
485
+ new fireauth . Auth . EmulatorConfigChangeEvent ( this . emulatorConfig_ ) ) ;
486
+ }
400
487
401
488
402
489
/**
@@ -449,7 +536,10 @@ fireauth.Auth.prototype.initAuthEventManager_ = function() {
449
536
// By this time currentUser should be ready if available and will be able
450
537
// to resolve linkWithRedirect if detected.
451
538
self . authEventManager_ = fireauth . AuthEventManager . getManager (
452
- authDomain , apiKey , self . app_ ( ) . name ) ;
539
+ authDomain ,
540
+ apiKey ,
541
+ self . app_ ( ) . name ,
542
+ self . emulatorConfig_ ) ;
453
543
// Subscribe Auth instance.
454
544
self . authEventManager_ . subscribe ( self ) ;
455
545
// Subscribe current user by enabling popup and redirect on that user.
@@ -471,7 +561,10 @@ fireauth.Auth.prototype.initAuthEventManager_ = function() {
471
561
/** @type {!fireauth.AuthUser } */ ( self . redirectUser_ ) ) ;
472
562
// Set the user Firebase frameworks for the redirect user.
473
563
self . setUserFramework_ (
474
- /** @type {!fireauth.AuthUser } */ ( self . redirectUser_ ) ) ;
564
+ /** @type {!fireauth.AuthUser } */ ( self . redirectUser_ ) ) ;
565
+ // Set the user Emulator configuration for the redirect user.
566
+ self . setUserEmulatorConfig_ (
567
+ /** @type {!fireauth.AuthUser } */ ( self . redirectUser_ ) ) ;
475
568
// Reference to redirect user no longer needed.
476
569
self . redirectUser_ = null ;
477
570
}
@@ -650,7 +743,8 @@ fireauth.Auth.prototype.signInWithPopup = function(provider) {
650
743
firebase . SDK_VERSION || null ,
651
744
null ,
652
745
null ,
653
- this . getTenantId ( ) ) ;
746
+ this . getTenantId ( ) ,
747
+ this . emulatorConfig_ ) ;
654
748
}
655
749
// The popup must have a name, otherwise when successive popups are triggered
656
750
// they will all render in the same instance and none will succeed since the
@@ -856,6 +950,9 @@ fireauth.Auth.prototype.signInWithIdTokenResponse =
856
950
options [ 'apiKey' ] = self . app_ ( ) . options [ 'apiKey' ] ;
857
951
options [ 'authDomain' ] = self . app_ ( ) . options [ 'authDomain' ] ;
858
952
options [ 'appName' ] = self . app_ ( ) . name ;
953
+ if ( self . emulatorConfig_ ) {
954
+ options [ 'emulatorConfig' ] = self . emulatorConfig_ ;
955
+ }
859
956
// Wait for state to be ready.
860
957
// This is used internally and is also used for redirect sign in so there is
861
958
// no need for waiting for redirect result to resolve since redirect result
@@ -911,6 +1008,9 @@ fireauth.Auth.prototype.setCurrentUser_ = function(user) {
911
1008
// Set the current frameworks used on the user and set current Auth instance
912
1009
// as the framework change dispatcher.
913
1010
this . setUserFramework_ ( user ) ;
1011
+ // If a user is available, set the emulator config on it and set current
1012
+ // Auth instance as emulator config change dispatcher.
1013
+ this . setUserEmulatorConfig_ ( user ) ;
914
1014
}
915
1015
} ;
916
1016
@@ -1001,7 +1101,7 @@ fireauth.Auth.prototype.initAuthState_ = function() {
1001
1101
var p = this . initRedirectUser_ ( ) . then ( function ( ) {
1002
1102
// Override user's authDomain with app's authDomain if there is a mismatch.
1003
1103
return /** @type {!fireauth.storage.UserManager } */ (
1004
- self . userStorageManager_ ) . getCurrentUser ( authDomain ) ;
1104
+ self . userStorageManager_ ) . getCurrentUser ( authDomain , self . emulatorConfig_ ) ;
1005
1105
} ) . then ( function ( user ) {
1006
1106
// Logged in user.
1007
1107
if ( user ) {
@@ -1179,6 +1279,22 @@ fireauth.Auth.prototype.setUserLanguage_ = function(user) {
1179
1279
} ;
1180
1280
1181
1281
1282
+ /**
1283
+ * Updates the emulator config on the provided user and configures the user
1284
+ * to listen to the Auth instance for any emulator config changes.
1285
+ * @param {!fireauth.AuthUser } user The user to whose emulator config needs
1286
+ * to be set.
1287
+ * @private
1288
+ */
1289
+ fireauth . Auth . prototype . setUserEmulatorConfig_ = function ( user ) {
1290
+ // Sets the current emulator config on the user.
1291
+ user . setEmulatorConfig ( this . emulatorConfig_ ) ;
1292
+ // Sets current Auth instance as emulator config change dispatcher on the
1293
+ // user.
1294
+ user . setEmulatorConfigChangeDispatcher ( this ) ;
1295
+ }
1296
+
1297
+
1182
1298
/**
1183
1299
* Handles user state changes.
1184
1300
* @param {!fireauth.AuthUser } user The user which triggered the state changes.
@@ -1679,6 +1795,15 @@ fireauth.Auth.prototype.app_ = function() {
1679
1795
} ;
1680
1796
1681
1797
1798
+ /**
1799
+ * @return {!fireauth.AuthSettings } The settings for this Auth object.
1800
+ * @private
1801
+ */
1802
+ fireauth . Auth . prototype . settings_ = function ( ) {
1803
+ return this [ 'settings' ] ;
1804
+ } ;
1805
+
1806
+
1682
1807
/**
1683
1808
* @return {!fireauth.RpcHandler } The RPC handler.
1684
1809
*/
0 commit comments