forked from firebase/firebaseui-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.js
More file actions
1176 lines (1113 loc) · 44.9 KB
/
Copy pathcommon.js
File metadata and controls
1176 lines (1113 loc) · 44.9 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 Common functions shared by handlers.
*/
goog.provide('firebaseui.auth.OAuthResponse');
goog.provide('firebaseui.auth.widget.handler.common');
goog.require('firebaseui.auth.Account');
goog.require('firebaseui.auth.PendingEmailCredential');
goog.require('firebaseui.auth.acClient');
goog.require('firebaseui.auth.idp');
goog.require('firebaseui.auth.log');
goog.require('firebaseui.auth.sni');
goog.require('firebaseui.auth.soy2.strings');
goog.require('firebaseui.auth.storage');
goog.require('firebaseui.auth.ui.element');
goog.require('firebaseui.auth.ui.page.Base');
goog.require('firebaseui.auth.ui.page.PasswordLinking');
goog.require('firebaseui.auth.ui.page.PasswordSignIn');
goog.require('firebaseui.auth.ui.page.UnrecoverableError');
goog.require('firebaseui.auth.util');
goog.require('firebaseui.auth.widget.Config');
goog.require('firebaseui.auth.widget.HandlerName');
goog.require('firebaseui.auth.widget.handler');
goog.require('goog.Promise');
goog.require('goog.array');
goog.require('goog.html.TrustedResourceUrl');
goog.require('goog.net.jsloader');
goog.require('goog.string.Const');
goog.forwardDeclare('firebaseui.auth.AuthUI');
/**
* @typedef {{
* oauthAccessToken: (null|string|undefined),
* oauthExpireIn: (null|number|undefined),
* oauthAuthorizationCode: (null|string|undefined)
* }}
*/
firebaseui.auth.OAuthResponse;
/**
* @define {string} The accountchooser.com client library URL.
*/
var ACCOUNTCHOOSER_SRC = '//www.gstatic.com/accountchooser/client.js';
/**
* @private {boolean} Whether uiShown callback should be triggered on callback
* in accountchooser.com select or add account regardless of
* accountchooser.com availability.
*/
firebaseui.auth.widget.handler.common.acForceUiShown_ = false;
/**
* @private {?goog.Promise} The promise that resolves when accountchooser.com
* client is loaded.
*/
firebaseui.auth.widget.handler.common.acLoader_ = null;
/**
* Loads the accountchooser.com client library if it is not loaded before and
* the user agent supports SNI.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {function()} callback The callback to invoke once it's loaded.
* @param {boolean=} opt_forceUiShownCallback Whether to force uiShown callback
* when accountchooser.com is unavailable.
*/
firebaseui.auth.widget.handler.common.loadAccountchooserJs = function(
app,
callback,
opt_forceUiShownCallback) {
firebaseui.auth.widget.handler.common.acForceUiShown_ =
!!opt_forceUiShownCallback;
// Load accountchooser.com client once and make sure callback waits until
// client is loaded.
if (!firebaseui.auth.widget.handler.common.acLoader_) {
if (typeof accountchooser == 'undefined' &&
firebaseui.auth.sni.isSupported()) {
// Not yet loaded but supported.
var src = goog.html.TrustedResourceUrl.fromConstant(
goog.string.Const.from(ACCOUNTCHOOSER_SRC));
firebaseui.auth.widget.handler.common.acLoader_ = goog.Promise.resolve(
goog.net.jsloader.safeLoad(src)).thenCatch(function() {});
} else {
// Either not supported by the browser or externally loaded.
firebaseui.auth.widget.handler.common.acLoader_ = goog.Promise.resolve();
}
}
// On ready, run callback.
firebaseui.auth.widget.handler.common.acLoader_.then(callback, callback);
};
/**
* Checks if an accountchooser.com invoked callback is available. If so, run it
* and passed a reference to the continue function, otherwise run the
* continue function directly.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {function()} continueCallback The continue function to run after
* invoking the accountchooser.com invoked callback.
*/
firebaseui.auth.widget.handler.common.accountChooserInvoked = function(
app, continueCallback) {
// Get accountchooser.com invoked callback.
var acInvokedCallback = app.getConfig().getAccountChooserInvokedCallback();
if (acInvokedCallback) {
// If accountchooser.com invoked callback provided, call it while passing
// continue function to it.
acInvokedCallback(continueCallback);
} else {
// No accountchooser.com invoked callback provided, continue callback.
continueCallback();
}
};
/**
* Checks if an accountchooser.com result callback is available. If so, run it
* while passing the result code to it.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {firebaseui.auth.widget.Config.AccountChooserResult} result The
* accountchooser.com result code.
* @param {function()} continueCallback The continue callback.
*/
firebaseui.auth.widget.handler.common.accountChooserResult = function(
app, result, continueCallback) {
// Get accountchooser.com result callback.
var acResultCallback = app.getConfig().getAccountChooserResultCallback();
// If available, call it and pass the result code to it.
if (acResultCallback) {
acResultCallback(result, continueCallback);
} else {
// No accountchooser.com result callback is provided, continue callback if
// provided.
continueCallback();
}
};
/**
* The callback to run when there is no pending accountchooser.com response.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element for the handler.
* @param {function()} uiShownCallback The uiShown callback URL to run when UI
* is shown.
* @param {boolean=} opt_disableSelectOnEmpty Whether to disable selecting an
* account when there are no pending results.
* @param {string=} opt_callbackUrl The URL to return to when the flow finishes.
* The default is current URL.
* @private
*/
firebaseui.auth.widget.handler.common.handleAcEmptyResponse_ = function(
app,
container,
uiShownCallback,
opt_disableSelectOnEmpty,
opt_callbackUrl) {
if (!!opt_disableSelectOnEmpty) {
// No pending accountchooser.com response, provider sign-in or callback
// handler should be rendered.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.CALLBACK, app, container);
// UI shown callback should be triggered.
if (firebaseui.auth.widget.handler.common.acForceUiShown_) {
uiShownCallback();
}
} else {
// If there is no pending accountchooser.com response and provider sign in
// is not to be rendered, try to select account from accountchooser.com.
// Do not redirect to accountchooser.com directly, instead package routine
// in continue callback function to be passed to accountchooser.com invoked
// handler.
var continueCallback = function() {
firebaseui.auth.acClient.trySelectAccount(
function(isAvailable) {
// On empty response, post accountchooser.com result (either empty
// or unavailable).
firebaseui.auth.widget.handler.common.accountChooserResult(
app,
isAvailable ?
firebaseui.auth.widget.Config.AccountChooserResult.EMPTY :
firebaseui.auth.widget.Config.AccountChooserResult
.UNAVAILABLE,
function() {
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.SIGN_IN, app,
container);
// If accountchooser.com is available or uiShown callback is
// forced, run uiShown callback.
if (isAvailable ||
firebaseui.auth.widget.handler.common.acForceUiShown_) {
uiShownCallback();
}
});
},
firebaseui.auth.storage.getRememberedAccounts(app.getAppId()),
opt_callbackUrl);
};
// Handle accountchooser.com invoked callback, pass continue callback for
// selected account on accountchooser.com.
firebaseui.auth.widget.handler.common.accountChooserInvoked(app,
continueCallback);
}
};
/**
* The callback to run when there is no pending accountchooser.com response.
*
* @param {firebaseui.auth.Account} account The account selected in
* accountchooser.com.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element for the handler.
* @param {function()} uiShownCallback The uiShown callback URL to run when UI
* is shown.
* @private
*/
firebaseui.auth.widget.handler.common.handleAcAccountSelectedResponse_ =
function(account, app, container, uiShownCallback) {
var errorHandler = function(error) {
var errorMessage = firebaseui.auth.widget.handler.common.getErrorMessage(
error);
// Depending on display mode, render relevant start page.
firebaseui.auth.widget.handler.common.handleSignInStart(
app,
container,
undefined,
errorMessage);
uiShownCallback();
};
var continueCallback = function() {
// If user selects an account from accountchooser.com, we shouldn't remember
// it locally. Otherwise, it will be out of sync if the user deletes it from
// accountchooser.com.
firebaseui.auth.storage.setRememberAccount(false, app.getAppId());
app.registerPending(app.getAuth().fetchProvidersForEmail(account.getEmail())
.then(function(providers) {
firebaseui.auth.widget.handler.common
.handleSignInFetchProvidersForEmail(
app,
container,
providers,
account.getEmail(),
account.getDisplayName() || undefined);
uiShownCallback();
}, errorHandler));
};
// Pass continue function to accountchooser.com result handler.
// Post accountchooser.com result: account selected.
firebaseui.auth.widget.handler.common.accountChooserResult(
app,
firebaseui.auth.widget.Config.AccountChooserResult.ACCOUNT_SELECTED,
continueCallback);
};
/**
* The callback to run when add account is selected in accountchooser.com
* response.
*
* @param {boolean} isAvailable Whether accountchooser.com is available.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element for the handler.
* @param {function()} uiShownCallback The uiShown callback URL to run when UI
* is shown.
* @private
*/
firebaseui.auth.widget.handler.common.handleAcAddAccountResponse_ =
function(isAvailable, app, container, uiShownCallback) {
var continueCallback = function() {
// This could be triggered even when accountchooser.com is unavailable.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.SIGN_IN, app, container);
if (isAvailable || firebaseui.auth.widget.handler.common.acForceUiShown_) {
uiShownCallback();
}
};
// Post accountchooser.com result: new account added or unavailable.
firebaseui.auth.widget.handler.common.accountChooserResult(
app,
isAvailable ?
firebaseui.auth.widget.Config.AccountChooserResult.ADD_ACCOUNT :
firebaseui.auth.widget.Config.AccountChooserResult.UNAVAILABLE,
continueCallback);
};
/**
* Selects account from accountchooser.
*
* @param {function():?firebaseui.auth.AuthUI} getApp The current FirebaseUI
* instance getter whose configuration is used.
* @param {Element} container The container DOM element for the handler.
* @param {boolean=} opt_disableSelectOnEmpty Whether to disable selecting an
* account when there are no pending results.
* @param {string=} opt_callbackUrl The URL to return to when the flow finishes.
* The default is current URL.
*/
firebaseui.auth.widget.handler.common.selectFromAccountChooser = function(
getApp,
container,
opt_disableSelectOnEmpty,
opt_callbackUrl) {
var uiShownCallback = function() {
var app = getApp();
if (!app) {
return;
}
var callback = app.getConfig().getUiShownCallback();
if (callback) {
callback();
}
};
firebaseui.auth.acClient.init(
function() {
var app = getApp();
if (!app) {
return;
}
firebaseui.auth.widget.handler.common.handleAcEmptyResponse_(
app,
container,
uiShownCallback,
opt_disableSelectOnEmpty,
opt_callbackUrl);
},
// Handle the account returned from accountchooser.com.
function(account) {
var app = getApp();
if (!app) {
return;
}
firebaseui.auth.widget.handler.common.handleAcAccountSelectedResponse_(
account, app, container, uiShownCallback);
},
// Handle adding an account.
function(isAvailable) {
var app = getApp();
if (!app) {
return;
}
firebaseui.auth.widget.handler.common.handleAcAddAccountResponse_(
isAvailable, app, container, uiShownCallback);
},
// Don't pass the supported provider list to accountchooser.com since
// Firebase doesn't need the provider meta info from accountchooser.
undefined,
goog.LOCALE,
getApp() && getApp().getConfig().getAcUiConfig());
};
/**
* Sets the user as signed in.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used and that has a user signed in.
* @param {firebaseui.auth.ui.page.Base} component The UI component.
* @param {?firebase.auth.AuthCredential} credential The auth credential
* object.
* @param {?firebase.User=} opt_user The current temporary user, provided if
* the user was already signed out from the temporary auth instance.
* @param {boolean=} opt_alreadySignedIn Whether user already signed in on
* external auth instance.
* @package
*/
firebaseui.auth.widget.handler.common.setLoggedIn =
function(app, component, credential, opt_user, opt_alreadySignedIn) {
if (!!opt_alreadySignedIn) {
// Already signed in on external auth instance.
firebaseui.auth.widget.handler.common.setUserLoggedInExternal_(
app,
component,
/** @type {!firebase.User} */ (app.getExternalAuth().currentUser),
credential);
return;
}
// This should not occur.
if (!credential) {
throw new Error('No credential found!');
}
var outputCred = credential;
// If the passed credential is a password credential, do not return it to the
// developer in signInSuccess callback.
if (credential['providerId'] &&
credential['providerId'] == 'password') {
// Do not return password credential to developer.
outputCred = null;
}
// For any error, display in info bar message.
var onError = function(error) {
// Ignore error if cancelled by the client.
if (error['name'] && error['name'] == 'cancel') {
return;
}
// Check if the error was due to an expired credential.
// This may happen in the email mismatch case where the user waits more than
// an hour and then proceeds to sign in with the expired credential.
// Display the relevant error message in this case and return the user to
// the sign-in start page.
if (firebaseui.auth.widget.handler.common.isCredentialExpired(error)) {
var container = component.getContainer();
// DIspose any existing component.
component.dispose();
// Call widget sign-in start handler with the expired credential error.
firebaseui.auth.widget.handler.common.handleSignInStart(
app,
container,
undefined,
firebaseui.auth.soy2.strings.errorExpiredCredential().toString());
} else {
var errorMessage = (error && error['message']) || '';
if (error['code']) {
// Firebase auth error.
errorMessage =
firebaseui.auth.widget.handler.common.getErrorMessage(error);
}
// Show error message in the info bar.
component.showInfoBar(errorMessage);
}
};
// In some cases like email mismatch, the temporary user may be signed out.
// In that case, get the current temporary user directly.
var tempUser = app.getAuth().currentUser || opt_user;
if (!tempUser) {
// Shouldn't happen as we're only calling this method internally.
throw new Error('User not logged in.');
}
// Sign out from internal auth instance before signing in to external
// instance.
app.registerPending(app.getAuth().signOut().then(function() {
// Save before signing in to developer's auth instance to make sure account
// is saved without risking interruption from onAuthStateChanged.
var account = new firebaseui.auth.Account(
tempUser['email'],
tempUser['displayName'],
tempUser['photoURL'],
outputCred && outputCred['providerId']);
// Remember account. If there is no user preference, remember account by
// default.
if (!firebaseui.auth.storage.hasRememberAccount(app.getAppId()) ||
firebaseui.auth.storage.isRememberAccount(app.getAppId())) {
firebaseui.auth.storage.rememberAccount(account, app.getAppId());
}
firebaseui.auth.storage.removeRememberAccount(app.getAppId());
// After successful sign out from internal instance, sign in with credential
// to the developer provided auth instance. Use the credential passed.
app.registerPending(app.getExternalAuth().signInWithCredential(
/** @type {!firebase.auth.AuthCredential} */ (credential))
.then(function(user) {
firebaseui.auth.widget.handler.common.setUserLoggedInExternal_(
app, component, user, outputCred);
// Catch error when signInSuccessUrl is required and not provided.
}, onError).then(function() {}, onError));
}, onError));
};
/**
* Completes the sign in operation assuming the current user is already signed
* in on the external auth instance. This routine will not try to remember the
* user account.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used and that has a user signed in.
* @param {firebaseui.auth.ui.page.Base} component The UI component.
* @param {!firebase.User} user The current user, provided signed in on the
* external auth instance.
* @param {?firebase.auth.AuthCredential} credential The auth credential
* object.
* @private
*/
firebaseui.auth.widget.handler.common.setUserLoggedInExternal_ =
function(app, component, user, credential) {
// Finish the flow by redirecting to sign-in success URL.
var callback = app.getConfig().getSignInSuccessCallback();
// Get redirect URL if it exists in non persistent storage.
// If sign-in success callback defined, pass redirect URL as third
// parameter.
// If not defined, override signInSuccessUrl with redirect URL value.
var redirectUrl = firebaseui.auth.storage.getRedirectUrl(
app.getAppId()) || undefined;
// Clear redirect URL from storage if available.
firebaseui.auth.storage.removeRedirectUrl(app.getAppId());
// Whether widget is redirecting. Initialize to false.
var isRedirecting = false;
if (firebaseui.auth.util.hasOpener()) {
// Popup sign in.
if (!callback ||
callback(
/** @type {!firebase.User} */ (user),
credential,
redirectUrl)) {
// Whether sign-in widget is redirecting.
isRedirecting = true;
// signInSuccessUrl is only required if there's no callback or it
// returns true, and if there's no redirectUrl present.
firebaseui.auth.util.openerGoTo(
firebaseui.auth.widget.handler.common.getSignedInRedirectUrl_(
app, redirectUrl));
}
if (!callback) {
// If the developer supplies a callback, do not close the popup
// window. Should be closed manually by the developer.
firebaseui.auth.util.close(window);
}
} else {
// Normal sign in.
if (!callback ||
callback(
/** @type {!firebase.User} */ (user),
credential,
redirectUrl)) {
// Sign-in widget is redirecting.
isRedirecting = true;
// signInSuccessUrl is only required if there's no callback or it
// returns true, and if there's no redirectUrl present.
firebaseui.auth.util.goTo(
firebaseui.auth.widget.handler.common.getSignedInRedirectUrl_(
app, redirectUrl));
}
}
// Dispose UI if not already disposed and not redirecting.
// If the widget is redirecting, it provides better UX to keep the loader
// showing until the page redirects. Otherwise, (most likely operating in
// single page mode), hide any remaining widget UI component.
if (!isRedirecting) {
app.reset();
}
};
/**
* Returns the redirect URL for a successful sign-in, when required. It will
* raise an error if none is found.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance.
* @param {string=} opt_redirectUrl An optional redirect URL coming from
* temporary storage.
* @return {string} The redirect URL to use.
* @private
*/
firebaseui.auth.widget.handler.common.getSignedInRedirectUrl_ =
function(app, opt_redirectUrl) {
var redirectUrl = opt_redirectUrl || app.getConfig().getSignInSuccessUrl();
if (!redirectUrl) {
throw new Error('No redirect URL has been found. You must either specify ' +
'a signInSuccessUrl in the configuration, pass in a redirect URL to t' +
'he widget URL, or return false from the callback.');
}
return redirectUrl;
};
/**
* Gets the display message for the error code.
* @param {*} error The error.
* @return {string} The display error message.
* @package
*/
firebaseui.auth.widget.handler.common.getErrorMessage = function(error) {
// Try to get an error message from the strings file, or fall back to the
// error message from the Firebase SDK if none is found.
var message =
firebaseui.auth.soy2.strings.error({code: error['code']}).toString();
if (message) {
return message;
}
// Tries to parse the JSON. If successful, display a generic error message.
try {
JSON.parse(error['message']);
firebaseui.auth.log.error('Internal error: ' + error['message']);
return firebaseui.auth.soy2.strings.internalError().toString();
} catch(e) {
// Otherwise the message must contain some info.
return error['message'];
}
};
/**
* Returns whether the error provided corresponds to a sign-in attempt with an
* expired OAuth credential.
* @param {*} error The error.
* @return {boolean} Whether the error returned is due to the OAuth credential
* being expired.
* @package
*/
firebaseui.auth.widget.handler.common.isCredentialExpired = function(error) {
// Check if the error is thrown due to the OAuth credential being expired.
// In that case an internal error code is thrown and the server response is
// serialized.
// TODO: update this error check when Firebase auth backend provides a
// dedicated error code instead of this hack.
var message = error['message'];
try {
// Check if the error message is a serialized json.
var errorDetails = JSON.parse(message);
// If so parse the internal message in the error.
var internalMessage = (errorDetails['error'] || {})['message'] || '';
// Expired Facebook access token:
// "invalid access_token, error code 43."
// Expired Google access token:
// "Invalid Idp Response: access_token is invalid"
// Look for invalid access_token pattern.
var re = new RegExp('invalid.+(access|id)_token');
var matches = internalMessage.toLowerCase().match(re);
if (matches && matches.length) {
// Match found, return true.
return true;
}
} catch(e) {}
return false;
};
/**
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {string} providerId The provider ID of the selected IdP.
* @param {?string=} opt_email The optional email to try to sign in with.
* @return {!firebase.auth.AuthProvider} The corresponding Firebase Auth
* provider with additional scopes and custom parameters.
* @private
*/
firebaseui.auth.widget.handler.common.getAuthProvider_ = function(
app, providerId, opt_email) {
// Construct provider and pass additional scopes.
var provider = firebaseui.auth.idp.getAuthProvider(providerId);
// Provider must be provided for any action to be taken.
if (!provider) {
// This shouldn't happen.
throw new Error('Invalid Firebase Auth provider!');
}
// Get additional scopes for requested provider.
var additionalScopes =
app.getConfig().getProviderAdditionalScopes(providerId);
// Some providers like Twitter do not accept additional scopes.
if (provider && provider['addScope']) {
// Add every requested additional scope to the provider.
for (var i = 0; i < additionalScopes.length; i++) {
provider['addScope'](additionalScopes[i]);
}
}
// Get custom parameters for the selected provider.
var customParameters =
app.getConfig().getProviderCustomParameters(providerId);
// If Google provider is requested and email is specified, pass OAuth
// parameter login_hint with that email.
// Only Google supports this parameter.
if (providerId == firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
// Only pass login_hint when email available.
opt_email) {
// In case no custom parameters are provided for google.com.
customParameters = customParameters || {};
// Add the login_hint.
customParameters['login_hint'] = opt_email;
}
// Add custom OAuth parameters if applicable for the current provider.
if (customParameters && provider && provider.setCustomParameters) {
provider.setCustomParameters(customParameters);
}
return provider;
};
/**
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component.
* @param {string} providerId The provider ID of the selected IdP.
* @param {?string=} opt_email The optional email to try to sign in with.
* @package
*/
firebaseui.auth.widget.handler.common.federatedSignIn = function(
app, component, providerId, opt_email) {
var container = component.getContainer();
var providerSigninFailedCallback = function(error) {
// TODO: align redirect and popup flow error handling for similar errors.
// Ignore error if cancelled by the client.
if (error['name'] && error['name'] == 'cancel') {
return;
}
firebaseui.auth.log.error('signInWithRedirect: ' + error['code']);
var errorMessage = firebaseui.auth.widget.handler.common.getErrorMessage(
error);
component.showInfoBar(errorMessage);
};
// Initialize the corresponding provider.
var provider = firebaseui.auth.widget.handler.common.getAuthProvider_(
app, providerId, opt_email);
// Redirect processor.
var processRedirect = function() {
app.registerPending(component.executePromiseRequest(
/** @type {function (): !goog.Promise} */ (
goog.bind(app.getAuth().signInWithRedirect, app.getAuth())),
[provider],
function() {
// Only run below logic if the environment is potentially a Cordova
// environment. This check is not required but will minimize the
// need to change existing tests that assertSignInWithRedirect.
if (firebaseui.auth.util.getScheme() !== 'file:') {
return;
}
// This will resolve in a Cordova environment. Result should be
// obtained from getRedirectResult and then treated like a
// signInWithPopup operation.
return app.registerPending(app.getAuth().getRedirectResult()
.then(function(result) {
// Pass result in promise to callback handler.
component.dispose();
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.CALLBACK,
app,
container,
goog.Promise.resolve(result));
}, providerSigninFailedCallback));
},
providerSigninFailedCallback));
};
// Get the sign-in flow.
var isRedirectMode = app.getConfig().getSignInFlow() ==
firebaseui.auth.widget.Config.SignInFlow.REDIRECT;
if (isRedirectMode) {
// Redirect flow.
processRedirect();
} else {
// Popup flow.
// During rpc, no progress bar should be displayed.
app.registerPending(app.getAuth().signInWithPopup(provider).then(
function(result) {
// Pass result in promise to callback handler.
component.dispose();
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.CALLBACK,
app,
container,
goog.Promise.resolve(result));
},
function(error) {
// Ignore error if cancelled by the client.
if (error['name'] && error['name'] == 'cancel') {
return;
}
switch (error['code']) {
case 'auth/popup-blocked':
// Popup blocked, switch to redirect flow as fallback.
processRedirect();
break;
case 'auth/popup-closed-by-user':
case 'auth/cancelled-popup-request':
// When popup is closed or when the user clicks another button,
// do nothing.
break;
case 'auth/network-request-failed':
case 'auth/too-many-requests':
case 'auth/user-cancelled':
// For no action errors like network error, just display in info
// bar in current component. A second attempt could still work.
component.showInfoBar(
firebaseui.auth.widget.handler.common.getErrorMessage(error));
break;
default:
// Either linking required errors or errors that are
// unrecoverable.
component.dispose();
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.CALLBACK,
app,
container,
goog.Promise.reject(error));
break;
}
}));
}
};
/**
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {firebaseui.auth.ui.page.PasswordLinking|
* firebaseui.auth.ui.page.PasswordSignIn}
* component The UI component.
* @package
*/
firebaseui.auth.widget.handler.common.verifyPassword =
function(app, component) {
// Check fields are valid.
var email = component.checkAndGetEmail();
var password = component.checkAndGetPassword();
if (!email) {
component.getEmailElement().focus();
return;
}
if (!password) {
component.getPasswordElement().focus();
return;
}
// Initialize an internal temporary password credential. This will be used
// to signInWithCredential to the developer provided auth instance on success.
// This credential will never be passed to developer or stored internally.
var emailPassCred =
firebase.auth.EmailAuthProvider.credential(email, password);
var showInvalidEmail = function(error) {
firebaseui.auth.ui.element.setValid(component.getEmailElement(), false);
firebaseui.auth.ui.element.show(component.getEmailErrorElement(),
firebaseui.auth.widget.handler.common.getErrorMessage(error));
};
var showInvalidPassword = function(error) {
firebaseui.auth.ui.element.setValid(component.getPasswordElement(), false);
firebaseui.auth.ui.element.show(component.getPasswordErrorElement(),
firebaseui.auth.widget.handler.common.getErrorMessage(error));
};
var showInfoBarWithError = function(error) {
component.showInfoBar(
firebaseui.auth.widget.handler.common.getErrorMessage(error));
};
app.registerPending(component.executePromiseRequest(
/** @type {function (): !goog.Promise} */ (
goog.bind(app.getAuth().signInWithEmailAndPassword, app.getAuth())),
[email, password],
function(user) {
// Pass password credential to complete sign-in to the original auth
// instance.
firebaseui.auth.widget.handler.common.setLoggedIn(
app, component, emailPassCred);
},
function(error) {
// Ignore error if cancelled by the client.
if (error['name'] && error['name'] == 'cancel') {
return;
}
switch (error['code']) {
case 'auth/email-exists':
showInvalidEmail(error);
break;
case 'auth/too-many-requests':
case 'auth/wrong-password':
showInvalidPassword(error);
break;
default:
firebaseui.auth.log.error('verifyPassword: ' + error['message']);
showInfoBarWithError(error);
break;
}
}
));
};
/**
* Displays an unrecoverable error message.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {!string} errorMessage The detailed error message to be displayed.
*/
firebaseui.auth.widget.handler.common.handleUnrecoverableError = function(
app, container, errorMessage) {
// Render the UI.
var component = new firebaseui.auth.ui.page.UnrecoverableError(errorMessage);
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
};
/**
* Helper function to check if a FirebaseUI instance only supports password
* providers.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @return {boolean} Whether only password providers are supported by the app's
* current configuration.
*/
firebaseui.auth.widget.handler.common.isPasswordProviderOnly = function(app) {
var providers = app.getConfig().getProviders();
return providers.length == 1 &&
providers[0] == firebase.auth.EmailAuthProvider.PROVIDER_ID;
};
/**
* Helper function to check if a FirebaseUI instance only supports phone
* providers.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @return {boolean} Whether only phone providers are supported by the app's
* current configuration.
*/
firebaseui.auth.widget.handler.common.isPhoneProviderOnly = function(app) {
var providers = app.getConfig().getProviders();
return providers.length == 1 &&
providers[0] == firebase.auth.PhoneAuthProvider.PROVIDER_ID;
};
/**
* Calls the appropriate sign-in start handler depending on display mode.
*
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {string=} opt_email The email to prefill.
* @param {string=} opt_infoBarMessage The message to show on info bar.
*/
firebaseui.auth.widget.handler.common.handleSignInStart = function(
app, container, opt_email, opt_infoBarMessage) {
if (firebaseui.auth.widget.handler.common.isPasswordProviderOnly(app)) {
// If info bar message is available, do not go to accountchooser.com since
// this is a result of some error in the flow and the error message must be
// displayed.
if (opt_infoBarMessage) {
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.SIGN_IN,
app,
container,
opt_email,
opt_infoBarMessage);
} else {
// Email auth provider is the only option, trigger that flow immediately
// instead of just showing a single sign-in with email button.
firebaseui.auth.widget.handler.common.handleSignInWithEmail(
app, container, opt_email);
}
} else if (
app && firebaseui.auth.widget.handler.common.isPhoneProviderOnly(app) &&
!opt_infoBarMessage) {
// Avoid an infinite loop by only skipping to phone auth if there's no
// error on phone auth rendering, eg recaptcha error when network down,
// which would trigger an info bar message.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.PHONE_SIGN_IN_START, app, container);
} else {
// For all other cases, show the provider sign-in screen.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.PROVIDER_SIGN_IN,
app,
container,
opt_infoBarMessage);
}
};
/**
* Starts the email first flow, when the user has provided her email.
* @param {firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {firebaseui.auth.ui.page.Base} component The UI component.
* @param {string} email The user's email
* @param {string=} opt_infoBarMessage The message to show on info bar.
*/
firebaseui.auth.widget.handler.common.handleStartEmailFirstFlow =
function(app, component, email, opt_infoBarMessage) {
var container = component.getContainer();
app.registerPending(component.executePromiseRequest(
/** @type {function (): !goog.Promise} */ (
goog.bind(app.getAuth().fetchProvidersForEmail, app.getAuth())),
[email],
function(providers) {
providers = /** @type {!Array<string>} */ (providers);
firebaseui.auth.storage.setRememberAccount(
app.getConfig().isAccountChooserEnabled(),
app.getAppId());