forked from firebase/firebaseui-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthui.js
More file actions
559 lines (491 loc) · 18 KB
/
Copy pathauthui.js
File metadata and controls
559 lines (491 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview FirebaseUI app builder.
* This helps support multiple instances of FirebaseUI configuration.
*/
goog.provide('firebaseui.auth.AuthUI');
goog.require('firebaseui.auth.EventDispatcher');
goog.require('firebaseui.auth.log');
goog.require('firebaseui.auth.storage');
goog.require('firebaseui.auth.util');
goog.require('firebaseui.auth.widget.Config');
goog.require('firebaseui.auth.widget.dispatcher');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleCallback');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleEmailChangeRevocation');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleEmailMismatch');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleEmailVerification');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleFederatedLinking');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleFederatedSignIn');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePasswordLinking');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePasswordRecovery');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePasswordReset');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePasswordSignIn');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePasswordSignUp');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePhoneSignInFinish');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handlePhoneSignInStart');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleProviderSignIn');
/** @suppress {extraRequire} */
goog.require('firebaseui.auth.widget.handler.handleSignIn');
goog.require('firebaseui.auth.widget.handler.startSignIn');
goog.require('goog.Promise');
goog.require('goog.array');
goog.require('goog.events');
goog.require('goog.events.EventType');
/**
* @param {!firebase.auth.Auth} auth The Firebase Auth instance.
* @param {string=} opt_appId The optional app id.
* @constructor
*/
firebaseui.auth.AuthUI = function(auth, opt_appId) {
/** @private {boolean} Whether the current instance is deleted. */
this.deleted_ = false;
// Check if an instance with the same key exists. If so, throw an error,
// otherwise, save that instance.
// Get the instance get.
var key = firebaseui.auth.AuthUI.getInstanceKey_(opt_appId);
if (firebaseui.auth.AuthUI.instances_[key]) {
// An instance exists for this key. Throw an error.
throw new Error(
'An AuthUI instance already exists for the key "' + key + '"');
} else {
// New instance, save reference to it.
firebaseui.auth.AuthUI.instances_[key] = this;
}
/** @private {!firebase.auth.Auth} The Firebase Auth instance. */
this.auth_ = auth;
var tempApp = firebase.initializeApp({
'apiKey': auth['app']['options']['apiKey'],
'authDomain': auth['app']['options']['authDomain']
}, auth['app']['name'] + firebaseui.auth.AuthUI.TEMP_APP_NAME_SUFFIX_);
/**
* @private {!firebase.auth.Auth} The temporary internal Firebase Auth
* instance.
*/
this.tempAuth_ = tempApp.auth();
/** @private {string|undefined} The optional app id. */
this.appId_ = opt_appId;
/** @private {!firebaseui.auth.widget.Config} The AuthUI configuration. */
this.config_ = new firebaseui.auth.widget.Config();
/**
* @private {?firebaseui.auth.EventDispatcher} The event dispatcher for
* triggering custom events on DOM objects.
*/
this.widgetEventDispatcher_ = null;
/**
* @private {?goog.Promise<!firebase.auth.UserCredential>} The result from
* any pending sign in with redirect operation.
*/
this.getRedirectResult_ = null;
/**
* @private {?Element} The current widget element container. Only one widget
* instance can be created per page.
*/
this.widgetElement_ = null;
/**
* @private {?firebaseui.auth.ui.page.Base} The currently rendered component.
*/
this.currentComponent_ = null;
/**
* @private {!Array<!goog.Promise|!firebase.Promise|!function()>} The array of
* pending promises or reset functions.
*/
this.pending_ = [];
};
/** Resets all internal globals. Used for testing only. */
firebaseui.auth.AuthUI.resetAllInternals = function() {
firebaseui.auth.AuthUI.instances_ = {};
firebaseui.auth.AuthUI.widgetElement_ = null;
};
/**
* @private {!Object.<!string, !firebaseui.auth.AuthUI>} Map containing the
* firebaseui.auth.AuthUI instances keyed by their app IDs.
*/
firebaseui.auth.AuthUI.instances_ = {};
/**
* Returns the instance key corresponding the appId provided.
* @param {?string=} opt_appId The optional app ID whose instance is to be
* provided.
* @return {string} The key corresponding to the provided app ID.
* @private
*/
firebaseui.auth.AuthUI.getInstanceKey_ = function(opt_appId) {
return opt_appId || firebaseui.auth.AuthUI.DEFAULT_INSTANCE_KEY_;
};
/**
* Returns the AuthUI instance corresponding to the appId provided.
* @param {?string=} opt_appId The optional app ID whose instance is to be
* provided.
* @return {?firebaseui.auth.AuthUI} The AuthUI instance corresponding to the
* app ID provided.
*/
firebaseui.auth.AuthUI.getInstance = function(opt_appId) {
var key = firebaseui.auth.AuthUI.getInstanceKey_(opt_appId);
if (firebaseui.auth.AuthUI.instances_[key]) {
return firebaseui.auth.AuthUI.instances_[key];
}
return null;
};
/**
* The suffix of the temp Auth instance app name.
* @const {string}
* @private
*/
firebaseui.auth.AuthUI.TEMP_APP_NAME_SUFFIX_ = '-firebaseui-temp';
/**
* The default instance key when no app ID is provided.
* @const {string}
* @private
*/
firebaseui.auth.AuthUI.DEFAULT_INSTANCE_KEY_ = '[DEFAULT]';
/**
* If sign-in succeeded, returns the signed in user. If sign-in was
* unsuccessful, fails with an error. If no redirect operation was called,
* returns a UserCredential with a null User.
*
* @return {!goog.Promise<!firebase.auth.UserCredential>} a UserCredential
* from the redirect-based sign-in flow.
*/
firebaseui.auth.AuthUI.prototype.getRedirectResult = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
if (!this.getRedirectResult_) {
this.getRedirectResult_ = goog.Promise.resolve(
this.getAuth().getRedirectResult());
}
return this.getRedirectResult_;
};
/**
* @param {!firebaseui.auth.ui.page.Base} component The current rendered
* component.
*/
firebaseui.auth.AuthUI.prototype.setCurrentComponent = function(component) {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
this.currentComponent_ = component;
};
/**
* The description of the error message raised when the widget element is not
* found during initialization.
* @const {string}
* @private
*/
firebaseui.auth.AuthUI.ELEMENT_NOT_FOUND_ = 'Could not find the FirebaseUI ' +
'widget element on the page.';
/**
* @private {?firebaseui.auth.AuthUI} The current widget AuthUI instance. Only
* one authui widget instance can be created per page.
*/
firebaseui.auth.AuthUI.widgetAuthUi_ = null;
/** @return {?firebaseui.auth.AuthUI} The current widget AuthUI instance. */
firebaseui.auth.AuthUI.getAuthUi = function() {
return firebaseui.auth.AuthUI.widgetAuthUi_;
};
/**
* @return {!firebase.auth.Auth} The temporary internal Firebase Auth instance.
* This is used to avoid triggering onAuthStateChanged observer on the
* developer provided Auth instance.
*/
firebaseui.auth.AuthUI.prototype.getAuth = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return this.tempAuth_;
};
/**
* @return {!firebase.auth.Auth} The original developer provided Firebase auth
* instance.
*/
firebaseui.auth.AuthUI.prototype.getExternalAuth = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return this.auth_;
};
/**
* @return {string|undefined} The app id if provided.
*/
firebaseui.auth.AuthUI.prototype.getAppId = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return this.appId_;
};
/**
* Returns true if there is any pending operations to be resolved by the widget.
* It is required in some cases like linking flows, where the user can become
* signed in and the widget still has to perform some operations (linking)
* before the sign-in state can be displayed in the app.
* When this flag is true, the widget HAS to be rendered.
* @return {boolean} Whether the app has pending operations to be performed.
*/
firebaseui.auth.AuthUI.prototype.isPending = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return firebaseui.auth.storage.hasPendingEmailCredential(this.getAppId());
};
/**
* Handles the FirebaseUI operation.
* An {@code Error} is thrown if the the developer tries to run this operation
* more than once.
*
* @param {string|!Element} element The container element or the query selector.
* @param {Object} config The configuration for sign-in button.
*/
firebaseui.auth.AuthUI.prototype.start = function(element, config) {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
var self = this;
// There is a problem when config in second call modifies accountchooser.com
// related config. eg. acUiConfig
// These changes will be ignored as only the first accountchooser.com related
// config will be applied.
this.setConfig(config);
var doc = goog.global.document;
// Wrap it in a onload callback to wait for the DOM element is rendered.
// If document already loaded, render immediately.
if (doc.readyState == 'complete') {
this.initElement_(element);
} else {
// Document not ready, wait for load before rendering.
goog.events.listenOnce(window, goog.events.EventType.LOAD, function() {
self.initElement_(element);
});
}
};
/**
* Initializes the FirebaseUI element.
* @param {string|!Element} element The container element or the query selector.
* @private
*/
firebaseui.auth.AuthUI.prototype.initElement_ = function(element) {
// Confirm element exists.
var container = firebaseui.auth.util.getElement(
element, firebaseui.auth.AuthUI.ELEMENT_NOT_FOUND_);
// Set the "lang" attribute; without this, there are subtle rendering errors
// like vowel capitalization in Turkish.
// Make sure the locale uses hyphens instead of strings.
var locale = goog.LOCALE.replace(/_/g, '-');
container.setAttribute('lang', locale);
// Only one auth instance can be rendered per page. This is because
// accountchooser.com callbacks are set once to the AuthUI instance that
// first calls them.
if (firebaseui.auth.AuthUI.widgetAuthUi_) {
// Already rendered, automatically reset.
// First check if there is a pending operation on that widget, if so,
// log a reset warning to the console.
if (firebaseui.auth.AuthUI.widgetAuthUi_.isPending()) {
var resetWarning = 'UI Widget is already rendered on the page and is ' +
'pending some user interaction. Only one widget instance can be ' +
'rendered per page. The previous instance has been automatically ' +
'reset.';
firebaseui.auth.log.warning(resetWarning);
}
firebaseui.auth.AuthUI.widgetAuthUi_.reset();
}
// Set widget AuthUI as current instance.
firebaseui.auth.AuthUI.widgetAuthUi_ = this;
this.widgetElement_ = container;
// Initialize widget page change listener.
this.initPageChangeListener_(container);
// Document already loaded, render on demand.
firebaseui.auth.widget.dispatcher.dispatchOperation(this, element);
};
/**
* Registers a pending promise or reset function.
* @param {?goog.Promise|?firebase.Promise|?function()} p The pending promise.
*/
firebaseui.auth.AuthUI.prototype.registerPending = function(p) {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
var self = this;
if (p) {
this.pending_.push(p);
var remove = function() {
goog.array.removeAllIf(self.pending_, function(ele) {
return ele == p;
});
};
// Remove pending promise on resolve or reject.
if (typeof p != 'function') {
p.then(remove, remove);
}
}
};
/**
* @return {function():?firebaseui.auth.AuthUI} The Firebase Auth instance
* getter.
*/
firebaseui.auth.AuthUI.prototype.getAuthUiGetter = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return firebaseui.auth.AuthUI.getAuthUi;
};
/** Reset rendered widget and removes it from display. */
firebaseui.auth.AuthUI.prototype.reset = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
// Remove the "lang" attribute that we set in start().
if (this.widgetElement_) {
this.widgetElement_.removeAttribute('lang');
}
// After reset, if the sign-in widget callback is called again, it should not
// resolve with the previous redirect result.
this.getRedirectResult_ =
/** @type {!goog.Promise<!firebase.auth.UserCredential>} */ (
goog.Promise.resolve({
'user': null,
'credential': null
}));
// Reset widget AuthUI if it is the current instance.
if (firebaseui.auth.AuthUI.widgetAuthUi_ == this) {
firebaseui.auth.AuthUI.widgetAuthUi_ = null;
}
this.widgetElement_ = null;
// Cancel all pending promises or run reset functions.
for (var i = 0; i < this.pending_.length; i++) {
if (typeof this.pending_[i] == 'function') {
/** @type{!function()} */ (this.pending_[i])();
} else {
// firebase.Promise may use the native Promise which does not have a
// cancel method. Only goog.Promise offers that.
if (this.pending_[i].cancel) {
this.pending_[i].cancel();
}
}
}
// Clear all pending promises and reset functions.
this.pending_ = [];
// Remove pending email credential.
firebaseui.auth.storage.removePendingEmailCredential(this.getAppId());
// Dispose any remaining widget UI.
if (this.currentComponent_) {
this.currentComponent_.dispose();
this.currentComponent_ = null;
}
// Reset current page id.
this.currentPageId_ = null;
};
/**
* Initializes the widget page change listener. On page change, triggers the UI
* changed callback if provided.
*
* @param {Element} element The widget container element.
* @private
*/
firebaseui.auth.AuthUI.prototype.initPageChangeListener_ = function(element) {
var self = this;
/** @private {?string} Current page id. */
this.currentPageId_ = null;
// Initialize the event dispatcher on the widget element.
this.widgetEventDispatcher_ = new firebaseui.auth.EventDispatcher(element);
// Register event dispatcher.
this.widgetEventDispatcher_.register();
// Listen to the pageEnter events.
goog.events.listen(
this.widgetEventDispatcher_,
'pageEnter',
function(event) {
// Get new page id.
var newPageId = event && event.pageId;
// If page change detected.
if (self.currentPageId_ != newPageId) {
// Get UI changed callback.
var uiChangedCallback = self.getConfig().getUiChangedCallback();
if (uiChangedCallback) {
// If UI changed callback provided, trigger it.
uiChangedCallback(self.currentPageId_, newPageId);
}
// Update current page id.
self.currentPageId_ = newPageId;
}
});
};
/**
* Updates the configuration and its descendants with the given value.
*
* @param {string} name The name of the configuration.
* @param {*} value The value of the configuration.
*/
firebaseui.auth.AuthUI.prototype.updateConfig = function(name, value) {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
this.config_.update(name, value);
};
/**
* Sets the app configuration.
* @param {Object} config The application configuration.
*/
firebaseui.auth.AuthUI.prototype.setConfig = function(config) {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
this.config_.setConfig(config);
};
/**
* @return {firebaseui.auth.widget.Config} The application configuration.
*/
firebaseui.auth.AuthUI.prototype.getConfig = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
return this.config_;
};
/**
* Triggers the sign-in flow.
*/
firebaseui.auth.AuthUI.prototype.signIn = function() {
// Check if instance is already destroyed.
this.checkIfDestroyed_();
firebaseui.auth.widget.handler.startSignIn(this);
};
/**
* Checks if the instance is destroyed. If so, throws an error.
* @private
*/
firebaseui.auth.AuthUI.prototype.checkIfDestroyed_ = function() {
if (this.deleted_) {
throw new Error('AuthUI instance is deleted!');
}
};
/**
* Destroys the AuthUI instance.
* @return {!firebase.Promise} The promise that resolves when the instance
* is successfully deleted.
*/
firebaseui.auth.AuthUI.prototype.delete = function() {
var self = this;
// Check if instance is already destroyed.
this.checkIfDestroyed_();
// Delete the temporary app instance.
return this.tempAuth_.app.delete().then(function() {
// Get instance key.
var key = firebaseui.auth.AuthUI.getInstanceKey_(self.getAppId());
// Delete any saved AuthUI instance.
delete firebaseui.auth.AuthUI.instances_[key];
// Reset current instance.
self.reset();
// Mark as deleted.
self.deleted_ = true;
});
};