Skip to content

Commit 2f756a5

Browse files
committed
Pull over more changes from internal repo
1 parent b1dae4b commit 2f756a5

14 files changed

+858
-170
lines changed

packages/auth/src/auth.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,57 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {
294294

295295
/**
296296
* Sets the emulator configuration (go/firebase-emulator-connection-api).
297-
/**
298-
* Sets the emulator configuration (go/firebase-emulator-connection-api).
299-
* @param {string} url The url for the Auth emulator.
300-
*/
297+
* @param {string} url The url for the Auth emulator.
298+
*/
301299
fireauth.Auth.prototype.useEmulator = function(url) {
302-
// Don't do anything if no change detected.
303-
if (!this.emulatorConfig_ || url !== this.emulatorConfig_.url) {
304-
console.warn("WARNING: You are using the Auth Emulator, which is" +
305-
" intended for local testing only. Do not use with" +
306-
" production credentials.");
307-
this.emulatorConfig_ = { url: 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 };
308306
// Disable app verification.
309-
this.settings_().setAppVerificationDisabledForTesting(true);
310-
// Update custom Firebase locale field.
307+
this.settings.setAppVerificationDisabledForTesting(true);
308+
// Update RPC handler endpoints.
311309
this.rpcHandler_.updateEmulatorConfig(this.emulatorConfig_);
312-
// Notify external language code change listeners.
310+
// Notify external event listeners.
313311
this.notifyEmulatorConfigListeners_();
314312
}
315313
}
316314

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+
const ele = goog.global.document.createElement('p');
326+
ele.innerText = 'Running in emulator mode. Do not use with production' +
327+
' credentials.';
328+
ele.style.position = 'fixed';
329+
ele.style.width = '100%';
330+
ele.style.backgroundColor = '#ffffff';
331+
ele.style.border = '.1em solid #000000';
332+
ele.style.color = '#ff0000';
333+
ele.style.bottom = '0px';
334+
ele.style.left = '0px';
335+
ele.style.margin = '0px';
336+
ele.style.zIndex = 10000;
337+
ele.style.textAlign = 'center';
338+
ele.classList.add('firebase-emulator-warning');
339+
goog.global.document.body.appendChild(ele);
340+
}
341+
}
342+
343+
317344
/**
318345
* @return {?fireauth.constants.EmulatorSettings}
319346
*/
320-
fireauth.Auth.prototype.getEmulatorConfig = function () {
347+
fireauth.Auth.prototype.getEmulatorConfig = function() {
321348
return this.emulatorConfig_;
322349
}
323350

@@ -451,7 +478,7 @@ fireauth.Auth.prototype.notifyLanguageCodeListeners_ = function() {
451478
* @private
452479
*/
453480
fireauth.Auth.prototype.notifyEmulatorConfigListeners_ = function() {
454-
// Notify external listeners on the language code change.
481+
// Notify external listeners on the emulator config change.
455482
this.dispatchEvent(
456483
new fireauth.Auth.EmulatorConfigChangeEvent(this.emulatorConfig_));
457484
}
@@ -877,7 +904,6 @@ fireauth.Auth.prototype.updateCurrentUser = function(user) {
877904
options['apiKey'] = this.app_().options['apiKey'];
878905
options['authDomain'] = this.app_().options['authDomain'];
879906
options['appName'] = this.app_().name;
880-
options['emulatorConfig'] = this.emulatorConfig_;
881907
var newUser = fireauth.AuthUser.copyUser(user, options,
882908
self.redirectUserStorageManager_, self.getFramework());
883909
return this.registerPendingPromise_(
@@ -922,7 +948,9 @@ fireauth.Auth.prototype.signInWithIdTokenResponse =
922948
options['apiKey'] = self.app_().options['apiKey'];
923949
options['authDomain'] = self.app_().options['authDomain'];
924950
options['appName'] = self.app_().name;
925-
options['emulatorConfig'] = self.emulatorConfig_;
951+
if (self.emulatorConfig_) {
952+
options['emulatorConfig'] = self.emulatorConfig_;
953+
}
926954
// Wait for state to be ready.
927955
// This is used internally and is also used for redirect sign in so there is
928956
// no need for waiting for redirect result to resolve since redirect result
@@ -1071,7 +1099,7 @@ fireauth.Auth.prototype.initAuthState_ = function() {
10711099
var p = this.initRedirectUser_().then(function() {
10721100
// Override user's authDomain with app's authDomain if there is a mismatch.
10731101
return /** @type {!fireauth.storage.UserManager} */ (
1074-
self.userStorageManager_).getCurrentUser(authDomain);
1102+
self.userStorageManager_).getCurrentUser(authDomain, self.emulatorConfig_);
10751103
}).then(function(user) {
10761104
// Logged in user.
10771105
if (user) {
@@ -1765,17 +1793,6 @@ fireauth.Auth.prototype.app_ = function() {
17651793
};
17661794

17671795

1768-
1769-
/**
1770-
* @return {!fireauth.AuthSettings} The AuthSettings object this auth object
1771-
* is connected to.
1772-
* @private
1773-
*/
1774-
fireauth.Auth.prototype.settings_ = function() {
1775-
return this['settings'];
1776-
}
1777-
1778-
17791796
/**
17801797
* @return {!fireauth.RpcHandler} The RPC handler.
17811798
*/

packages/auth/src/autheventmanager.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,18 @@ fireauth.AuthEventManager.KEY_SEPARATOR_ = ':';
672672
/**
673673
* @param {string} apiKey The API key for sending backend Auth requests.
674674
* @param {string} appName The Auth instance that initiated the Auth event.
675-
* @return {string} The key identifying the Auth event manager instance.
676-
* @private
677-
*/
678-
fireauth.AuthEventManager.getKey_ = function(apiKey, appName) {
679-
return apiKey + fireauth.AuthEventManager.KEY_SEPARATOR_ + appName;
680-
};
675+
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
676+
* configuration.
677+
* @return {string} The key identifying the Auth event manager instance.
678+
* @private
679+
*/
680+
fireauth.AuthEventManager.getKey_ = function(apiKey, appName, emulatorConfig) {
681+
var key = apiKey + fireauth.AuthEventManager.KEY_SEPARATOR_ + appName;
682+
if (emulatorConfig) {
683+
key = key + fireauth.AuthEventManager.KEY_SEPARATOR_ + emulatorConfig.url;
684+
}
685+
return key;
686+
}
681687

682688

683689
/**
@@ -690,18 +696,26 @@ fireauth.AuthEventManager.getKey_ = function(apiKey, appName) {
690696
* configuration.
691697
* @return {!fireauth.AuthEventManager} the requested manager instance.
692698
*/
693-
fireauth.AuthEventManager.getManager = function(authDomain, apiKey, appName, emulatorConfig) {
699+
fireauth.AuthEventManager.getManager = function (authDomain, apiKey, appName, emulatorConfig) {
694700
// Construct storage key.
695-
var key = fireauth.AuthEventManager.getKey_(apiKey, appName);
701+
var key = fireauth.AuthEventManager.getKey_(
702+
apiKey,
703+
appName,
704+
emulatorConfig
705+
);
696706
if (!fireauth.AuthEventManager.manager_[key]) {
697707
fireauth.AuthEventManager.manager_[key] =
698-
new fireauth.AuthEventManager(authDomain, apiKey, appName, emulatorConfig);
708+
new fireauth.AuthEventManager(
709+
authDomain,
710+
apiKey,
711+
appName,
712+
emulatorConfig
713+
);
699714
}
700715
return fireauth.AuthEventManager.manager_[key];
701716
};
702717

703718

704-
705719
/**
706720
* The interface that represents a specific type of Auth event processor.
707721
* @interface

packages/auth/src/authuser.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ fireauth.AuthUser.prototype.setLanguageCodeChangeDispatcher =
351351

352352

353353
/**
354-
* Listens to emulator config changes triggered by the provided dispatcher.
355-
* @param {?goog.events.EventTarget} dispatcher The emulator config changed
356-
* event dispatcher.
357-
*/
354+
* Listens to emulator config changes triggered by the provided dispatcher.
355+
* @param {?goog.events.EventTarget} dispatcher The emulator config changed
356+
* event dispatcher.
357+
*/
358358
fireauth.AuthUser.prototype.setEmulatorConfigChangeDispatcher = function(dispatcher) {
359359
// Remove any previous listener.
360360
if (this.emulatorConfigChangeEventDispatcher_) {
@@ -367,7 +367,7 @@ fireauth.AuthUser.prototype.setEmulatorConfigChangeDispatcher = function(dispatc
367367
this.emulatorConfigChangeEventDispatcher_ = dispatcher;
368368
// Using an event listener makes it easy for non-currentUsers to detect
369369
// emulator changes on the parent Auth instance. A developer could still
370-
// call APIs that require localization on signed out user references.
370+
// call APIs that require emulation on signed out user references.
371371
if (dispatcher) {
372372
goog.events.listen(
373373
dispatcher, fireauth.constants.AuthEventType.EMULATOR_CONFIG_CHANGED,
@@ -2423,7 +2423,8 @@ fireauth.AuthUser.fromPlainObject = function(user) {
24232423
var options = {
24242424
'apiKey': user['apiKey'],
24252425
'authDomain': user['authDomain'],
2426-
'appName': user['appName']
2426+
'appName': user['appName'],
2427+
'emulatorConfig': user['emulatorConfig']
24272428
};
24282429
// Convert to server response format. Constructor does not take
24292430
// stsTokenManager toPlainObject as that format is different than the return

packages/auth/src/cordovahandler.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,27 @@ goog.require('goog.crypt.Sha256');
5656
* @param {string} authDomain The application authDomain.
5757
* @param {string} apiKey The API key.
5858
* @param {string} appName The App name.
59-
* @param {?string=} opt_clientVersion The optional client version string.
60-
* @param {number=} opt_initialTimeout Initial Auth event timeout.
61-
* @param {number=} opt_redirectTimeout Redirect result timeout.
62-
* @param {?string=} opt_endpointId The endpoint ID (staging, test Gaia, etc).
59+
* @param {?string=} clientVersion The optional client version string.
60+
* @param {number=} initialTimeout Initial Auth event timeout.
61+
* @param {number=} redirectTimeout Redirect result timeout.
62+
* @param {?string=} endpointId The endpoint ID (staging, test Gaia, etc).
6363
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
6464
* configuration
6565
* @constructor
6666
* @implements {fireauth.OAuthSignInHandler}
6767
*/
6868
fireauth.CordovaHandler = function(authDomain, apiKey, appName,
69-
opt_clientVersion, opt_initialTimeout, opt_redirectTimeout,
70-
opt_endpointId, emulatorConfig) {
69+
clientVersion, initialTimeout, redirectTimeout, endpointId, emulatorConfig) {
7170
/** @private {string} The application authDomain. */
7271
this.authDomain_ = authDomain;
7372
/** @private {string} The application API key. */
7473
this.apiKey_ = apiKey;
7574
/** @private {string} The application name. */
7675
this.appName_ = appName;
7776
/** @private {?string} The client version */
78-
this.clientVersion_ = opt_clientVersion || null;
77+
this.clientVersion_ = clientVersion || null;
7978
/** @private {?string} The Auth endpoint ID. */
80-
this.endpointId_ = opt_endpointId || null;
79+
this.endpointId_ = endpointId || null;
8180
/**
8281
* @private @const {?fireauth.constants.EmulatorSettings|undefined}
8382
* The emulator configuration
@@ -109,10 +108,10 @@ fireauth.CordovaHandler = function(authDomain, apiKey, appName,
109108
*/
110109
this.authEventListeners_ = [];
111110
/** @private {number} The initial Auth event timeout. */
112-
this.initialTimeout_ = opt_initialTimeout ||
111+
this.initialTimeout_ = initialTimeout ||
113112
fireauth.CordovaHandler.INITIAL_TIMEOUT_MS_;
114113
/** @private {number} The return to app after redirect timeout. */
115-
this.redirectTimeout_ = opt_redirectTimeout ||
114+
this.redirectTimeout_ = redirectTimeout ||
116115
fireauth.CordovaHandler.REDIRECT_TIMEOUT_MS_;
117116
/**
118117
* @private {?goog.Promise} The last pending redirect promise. This is null if

packages/auth/src/iframeclient/ifchandler.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ fireauth.iframeclient.IframeUrlBuilder = function(authDomain, apiKey, appName, e
104104
null);
105105
}
106106
/**
107-
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
108-
*/
107+
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
108+
*/
109109
this.uri_ = uri;
110110
this.uri_.setParameterValue('apiKey', this.apiKey_);
111111
this.uri_.setParameterValue('appName', this.appName_);
@@ -774,6 +774,9 @@ fireauth.iframeclient.IfcHandler.prototype.getRpcHandler_ = function() {
774774
// Get the client Auth endpoint used.
775775
fireauth.constants.getEndpointConfig(this.endpointId_),
776776
this.fullClientVersion_);
777+
if (this.emulatorConfig_) {
778+
this.rpcHandler_.updateEmulatorConfig(this.emulatorConfig_);
779+
}
777780
}
778781
return this.rpcHandler_;
779782
};

packages/auth/src/rpchandler.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,10 @@ fireauth.RpcHandler.prototype.updateCustomLocaleHeader =
437437

438438

439439
/**
440-
* Updates the emulator configuration.
441-
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The new
442-
* emulator config.
443-
*/
440+
* Updates the emulator configuration.
441+
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The new
442+
* emulator config.
443+
*/
444444
fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
445445
if (!emulatorConfig) {
446446
return;
@@ -457,24 +457,21 @@ fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
457457
fireauth.RpcHandler.IDENTITY_PLATFORM_ENDPOINT_, emulatorConfig);
458458
}
459459

460-
461-
/**
462-
* Creates an endpoint URL intended for use by the emulator.
463-
*
464-
* According to go/firebase-auth-emulator-dd
465-
* @param {string} endpoint the production endpoint URL.
466-
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The emulator
467-
* config.
468-
* @return {string} The emulator endpoint URL.
469-
* @private
470-
*/
460+
/**
461+
* Creates an endpoint URL intended for use by the emulator.
462+
* @param {string} endpoint the production endpoint URL.
463+
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The emulator
464+
* config.
465+
* @return {string} The emulator endpoint URL.
466+
* @private
467+
*/
471468
fireauth.RpcHandler.generateEmululatorEndpointUrl_ = function(endpoint, emulatorConfig) {
472469
const uri = goog.Uri.parse(endpoint);
473-
const endpointUri = goog.Uri.parse(emulatorConfig.url);
470+
const emulatorUri = goog.Uri.parse(emulatorConfig.url);
474471
uri.setPath(uri.getDomain() + uri.getPath());
475-
uri.setScheme(endpointUri.getScheme());
476-
uri.setDomain(endpointUri.getDomain());
477-
uri.setPort(endpointUri.getPort());
472+
uri.setScheme(emulatorUri.getScheme());
473+
uri.setDomain(emulatorUri.getDomain());
474+
uri.setPort(emulatorUri.getPort());
478475
return uri.toString();
479476
}
480477

packages/auth/src/storageusermanager.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ goog.provide('fireauth.storage.UserManager');
6767

6868
goog.require('fireauth.AuthUser');
6969
goog.require('fireauth.authStorage');
70+
goog.require('fireauth.constants');
7071
goog.require('goog.Promise');
7172

7273

@@ -399,13 +400,14 @@ fireauth.storage.UserManager.prototype.removeCurrentUser = function() {
399400

400401

401402
/**
402-
* @param {?string=} opt_authDomain The optional Auth domain to override if
403+
* @param {?string=} authDomain The optional Auth domain to override if
403404
* provided.
405+
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The current
406+
* emulator config to use in user requests.
404407
* @return {!goog.Promise<?fireauth.AuthUser>} A promise that resolves with
405408
* the stored current user for the provided app ID.
406409
*/
407-
fireauth.storage.UserManager.prototype.getCurrentUser =
408-
function(opt_authDomain) {
410+
fireauth.storage.UserManager.prototype.getCurrentUser = function(authDomain, emulatorConfig) {
409411
var self = this;
410412
// Wait for any pending persistence change to be resolved.
411413
return this.waitForReady_(function() {
@@ -420,8 +422,11 @@ fireauth.storage.UserManager.prototype.getCurrentUser =
420422
// authDomain for the purpose of linking with a popup. The loaded user
421423
// (stored without the authDomain) must have this field updated with
422424
// the current authDomain.
423-
if (response && opt_authDomain) {
424-
response['authDomain'] = opt_authDomain;
425+
if (response && authDomain) {
426+
response['authDomain'] = authDomain;
427+
}
428+
if (response && emulatorConfig) {
429+
response['emulatorConfig'] = emulatorConfig;
425430
}
426431
return fireauth.AuthUser.fromPlainObject(response || {});
427432
});

0 commit comments

Comments
 (0)