@@ -312,6 +312,49 @@ function onSignInWithEmailAndPassword() {
312
312
}
313
313
314
314
315
+ /**
316
+ * Signs in a user with an email link.
317
+ */
318
+ function onSignInWithEmailLink ( ) {
319
+ var email = $ ( '#sign-in-with-email-link-email' ) . val ( ) ;
320
+ var link = $ ( '#sign-in-with-email-link-link' ) . val ( ) || undefined ;
321
+ if ( auth . isSignInWithEmailLink ( link ) ) {
322
+ auth . signInWithEmailLink ( email , link ) . then ( onAuthSuccess , onAuthError ) ;
323
+ } else {
324
+ alertError ( 'Sign in link is invalid' ) ;
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Links a user with an email link.
330
+ */
331
+ function onLinkWithEmailLink ( ) {
332
+ var email = $ ( '#link-with-email-link-email' ) . val ( ) ;
333
+ var link = $ ( '#link-with-email-link-link' ) . val ( ) || undefined ;
334
+ var credential = firebase . auth . EmailAuthProvider
335
+ . credentialWithLink ( email , link ) ;
336
+ activeUser ( ) . linkAndRetrieveDataWithCredential ( credential )
337
+ . then ( onAuthUserCredentialSuccess , onAuthError ) ;
338
+ }
339
+
340
+
341
+ /**
342
+ * Re-authenticate a user with email link credential.
343
+ */
344
+ function onReauthenticateWithEmailLink ( ) {
345
+ var email = $ ( '#link-with-email-link-email' ) . val ( ) ;
346
+ var link = $ ( '#link-with-email-link-link' ) . val ( ) || undefined ;
347
+ var credential = firebase . auth . EmailAuthProvider
348
+ . credentialWithLink ( email , link ) ;
349
+ activeUser ( ) . reauthenticateAndRetrieveDataWithCredential ( credential )
350
+ . then ( function ( result ) {
351
+ logAdditionalUserInfo ( result ) ;
352
+ refreshUserData ( ) ;
353
+ alertSuccess ( 'User reauthenticated!' ) ;
354
+ } , onAuthError ) ;
355
+ }
356
+
357
+
315
358
/**
316
359
* Signs in with a custom token.
317
360
* @param {DOMEvent } event HTML DOM event returned by the listener.
@@ -581,6 +624,52 @@ function onUpdateProfile() {
581
624
}
582
625
583
626
627
+ /**
628
+ * Sends sign in with email link to the user.
629
+ */
630
+ function onSendSignInLinkToEmail ( ) {
631
+ var email = $ ( '#sign-in-with-email-link-email' ) . val ( ) ;
632
+ auth . sendSignInLinkToEmail ( email , getActionCodeSettings ( ) ) . then ( function ( ) {
633
+ alertSuccess ( 'Email sent!' ) ;
634
+ } , onAuthError ) ;
635
+ }
636
+
637
+ /**
638
+ * Sends sign in with email link to the user and pass in current url.
639
+ */
640
+ function onSendSignInLinkToEmailCurrentUrl ( ) {
641
+ var email = $ ( '#sign-in-with-email-link-email' ) . val ( ) ;
642
+ var actionCodeSettings = {
643
+ 'url' : window . location . href ,
644
+ 'handleCodeInApp' : true
645
+ } ;
646
+
647
+ auth . sendSignInLinkToEmail ( email , actionCodeSettings ) . then ( function ( ) {
648
+ if ( 'localStorage' in window && window [ 'localStorage' ] !== null ) {
649
+ window . localStorage . setItem (
650
+ 'emailForSignIn' ,
651
+ // Save the email and the timestamp.
652
+ JSON . stringify ( {
653
+ email : email ,
654
+ timestamp : new Date ( ) . getTime ( )
655
+ } ) ) ;
656
+ }
657
+ alertSuccess ( 'Email sent!' ) ;
658
+ } , onAuthError ) ;
659
+ }
660
+
661
+
662
+ /**
663
+ * Sends email link to link the user.
664
+ */
665
+ function onSendLinkEmailLink ( ) {
666
+ var email = $ ( '#link-with-email-link-email' ) . val ( ) ;
667
+ auth . sendSignInLinkToEmail ( email , getActionCodeSettings ( ) ) . then ( function ( ) {
668
+ alertSuccess ( 'Email sent!' ) ;
669
+ } , onAuthError ) ;
670
+ }
671
+
672
+
584
673
/**
585
674
* Sends password reset email to the user.
586
675
*/
@@ -615,6 +704,41 @@ function onConfirmPasswordReset() {
615
704
}
616
705
617
706
707
+ /**
708
+ * Gets the list of IDPs that can be used to log in for the given email address.
709
+ */
710
+ function onFetchProvidersForEmail ( ) {
711
+ var email = $ ( '#fetch-providers-email' ) . val ( ) ;
712
+ auth . fetchProvidersForEmail ( email ) . then ( function ( providers ) {
713
+ log ( 'Providers for ' + email + ' :' ) ;
714
+ log ( providers ) ;
715
+ if ( providers . length == 0 ) {
716
+ alertSuccess ( 'Providers for ' + email + ': N/A' ) ;
717
+ } else {
718
+ alertSuccess ( 'Providers for ' + email + ': ' + providers . join ( ', ' ) ) ;
719
+ }
720
+ } , onAuthError ) ;
721
+ }
722
+
723
+
724
+ /**
725
+ * Gets the list of possible sign in methods for the given email address.
726
+ */
727
+ function onFetchSignInMethodsForEmail ( ) {
728
+ var email = $ ( '#fetch-providers-email' ) . val ( ) ;
729
+ auth . fetchSignInMethodsForEmail ( email ) . then ( function ( signInMethods ) {
730
+ log ( 'Sign in methods for ' + email + ' :' ) ;
731
+ log ( signInMethods ) ;
732
+ if ( signInMethods . length == 0 ) {
733
+ alertSuccess ( 'Sign In Methods for ' + email + ': N/A' ) ;
734
+ } else {
735
+ alertSuccess (
736
+ 'Sign In Methods for ' + email + ': ' + signInMethods . join ( ', ' ) ) ;
737
+ }
738
+ } , onAuthError ) ;
739
+ }
740
+
741
+
618
742
/**
619
743
* Fetches and logs the user's providers data.
620
744
*/
@@ -966,13 +1090,43 @@ function getParameterByName(name) {
966
1090
* the input field for the confirm email verification process.
967
1091
*/
968
1092
function populateActionCodes ( ) {
1093
+ var emailForSignIn = null ;
1094
+ var signInTime = 0 ;
1095
+ if ( 'localStorage' in window && window [ 'localStorage' ] !== null ) {
1096
+ try {
1097
+ // Try to parse as JSON first using new storage format.
1098
+ var emailForSignInData =
1099
+ JSON . parse ( window . localStorage . getItem ( 'emailForSignIn' ) ) ;
1100
+ emailForSignIn = emailForSignInData [ 'email' ] || null ;
1101
+ signInTime = emailForSignInData [ 'timestamp' ] || 0 ;
1102
+ } catch ( e ) {
1103
+ // JSON parsing failed. This means the email is stored in the old string
1104
+ // format.
1105
+ emailForSignIn = window . localStorage . getItem ( 'emailForSignIn' ) ;
1106
+ }
1107
+ if ( emailForSignIn ) {
1108
+ // Clear old codes. Old format codes should be cleared immediately.
1109
+ if ( new Date ( ) . getTime ( ) - signInTime >= 1 * 24 * 3600 * 1000 ) {
1110
+ // Remove email from storage.
1111
+ window . localStorage . removeItem ( 'emailForSignIn' ) ;
1112
+ }
1113
+ }
1114
+ }
969
1115
var actionCode = getParameterByName ( 'oobCode' ) ;
970
1116
if ( actionCode != null ) {
971
1117
var mode = getParameterByName ( 'mode' ) ;
972
1118
if ( mode == 'verifyEmail' ) {
973
1119
$ ( '#email-verification-code' ) . val ( actionCode ) ;
974
1120
} else if ( mode == 'resetPassword' ) {
975
1121
$ ( '#password-reset-code' ) . val ( actionCode ) ;
1122
+ } else if ( mode == 'signIn' ) {
1123
+ if ( emailForSignIn ) {
1124
+ $ ( '#sign-in-with-email-link-email' ) . val ( emailForSignIn ) ;
1125
+ $ ( '#sign-in-with-email-link-link' ) . val ( window . location . href ) ;
1126
+ onSignInWithEmailLink ( ) ;
1127
+ // Remove email from storage as the code is only usable once.
1128
+ window . localStorage . removeItem ( 'emailForSignIn' ) ;
1129
+ }
976
1130
} else {
977
1131
$ ( '#email-verification-code' ) . val ( actionCode ) ;
978
1132
$ ( '#password-reset-code' ) . val ( actionCode ) ;
@@ -1153,11 +1307,19 @@ function initApp(){
1153
1307
e . preventDefault ( ) ;
1154
1308
}
1155
1309
} ) ;
1310
+ $ ( '#sign-in-with-email-link' ) . click ( onSignInWithEmailLink ) ;
1311
+ $ ( '#link-with-email-link' ) . click ( onLinkWithEmailLink ) ;
1312
+ $ ( '#reauth-with-email-link' ) . click ( onReauthenticateWithEmailLink ) ;
1156
1313
1157
1314
$ ( '#change-email' ) . click ( onChangeEmail ) ;
1158
1315
$ ( '#change-password' ) . click ( onChangePassword ) ;
1159
1316
$ ( '#update-profile' ) . click ( onUpdateProfile ) ;
1160
1317
1318
+ $ ( '#send-sign-in-link-to-email' ) . click ( onSendSignInLinkToEmail ) ;
1319
+ $ ( '#send-sign-in-link-to-email-current-url' )
1320
+ . click ( onSendSignInLinkToEmailCurrentUrl ) ;
1321
+ $ ( '#send-link-email-link' ) . click ( onSendLinkEmailLink ) ;
1322
+
1161
1323
$ ( '#send-password-reset-email' ) . click ( onSendPasswordResetEmail ) ;
1162
1324
$ ( '#verify-password-reset-code' ) . click ( onVerifyPasswordResetCode ) ;
1163
1325
$ ( '#confirm-password-reset' ) . click ( onConfirmPasswordReset ) ;
@@ -1202,6 +1364,9 @@ function initApp(){
1202
1364
1203
1365
$ ( '#set-language-code' ) . click ( onSetLanguageCode ) ;
1204
1366
$ ( '#use-device-language' ) . click ( onUseDeviceLanguage ) ;
1367
+
1368
+ $ ( '#fetch-providers-for-email' ) . click ( onFetchProvidersForEmail ) ;
1369
+ $ ( '#fetch-sign-in-methods-for-email' ) . click ( onFetchSignInMethodsForEmail ) ;
1205
1370
}
1206
1371
1207
1372
$ ( initApp ) ;
0 commit comments