@@ -247,6 +247,10 @@ function showMultiFactorStatus(activeUser) {
247247 const label = info && ( info . displayName || info . uid ) ;
248248 if ( label ) {
249249 $ ( '#enrolled-factors-drop-down' ) . removeClass ( 'open' ) ;
250+ // Set the last user, in case the current user is logged out.
251+ // This can happen if the MFA option being unenrolled is the one that was most recently enrolled into.
252+ // See - https://github.com/firebase/firebase-js-sdk/issues/3233
253+ setLastUser ( activeUser ) ;
250254 mfaUser . unenroll ( info ) . then ( ( ) => {
251255 refreshUserData ( ) ;
252256 alertSuccess ( 'Multi-factor successfully unenrolled.' ) ;
@@ -278,6 +282,9 @@ function onAuthError(error) {
278282 handleMultiFactorSignIn ( getMultiFactorResolver ( auth , error ) ) ;
279283 } else {
280284 alertError ( 'Error: ' + error . code ) ;
285+ if ( error . code === 'auth/user-token-expired' ) {
286+ alertError ( 'Token expired, please reauthenticate.' ) ;
287+ }
281288 }
282289}
283290
@@ -403,13 +410,41 @@ function onLinkWithEmailLink() {
403410 * Re-authenticate a user with email link credential.
404411 */
405412function onReauthenticateWithEmailLink ( ) {
413+ if ( ! activeUser ( ) ) {
414+ alertError (
415+ 'No user logged in. Select the "Last User" tab to reauth the previous user.'
416+ ) ;
417+ return ;
418+ }
406419 const email = $ ( '#link-with-email-link-email' ) . val ( ) ;
407420 const link = $ ( '#link-with-email-link-link' ) . val ( ) || undefined ;
408421 const credential = EmailAuthProvider . credentialWithLink ( email , link ) ;
422+ // This will not set auth.currentUser to lastUser if the lastUser is reauthenticated.
409423 reauthenticateWithCredential ( activeUser ( ) , credential ) . then ( result => {
410424 logAdditionalUserInfo ( result ) ;
411425 refreshUserData ( ) ;
412- alertSuccess ( 'User reauthenticated!' ) ;
426+ alertSuccess ( 'User reauthenticated with email link!' ) ;
427+ } , onAuthError ) ;
428+ }
429+
430+ /**
431+ * Re-authenticate a user with email and password.
432+ */
433+ function onReauthenticateWithEmailAndPassword ( ) {
434+ if ( ! activeUser ( ) ) {
435+ alertError (
436+ 'No user logged in. Select the "Last User" tab to reauth the previous user.'
437+ ) ;
438+ return ;
439+ }
440+ const email = $ ( '#signin-email' ) . val ( ) ;
441+ const password = $ ( '#signin-password' ) . val ( ) ;
442+ const credential = EmailAuthProvider . credential ( email , password ) ;
443+ // This will not set auth.currentUser to lastUser if the lastUser is reauthenticated.
444+ reauthenticateWithCredential ( activeUser ( ) , credential ) . then ( result => {
445+ logAdditionalUserInfo ( result ) ;
446+ refreshUserData ( ) ;
447+ alertSuccess ( 'User reauthenticated with email/password!' ) ;
413448 } , onAuthError ) ;
414449}
415450
@@ -1264,7 +1299,9 @@ function signInWithPopupRedirect(provider) {
12641299 break ;
12651300 case 'reauthenticate' :
12661301 if ( ! activeUser ( ) ) {
1267- alertError ( 'No user logged in.' ) ;
1302+ alertError (
1303+ 'No user logged in. Select the "Last User" tab to reauth the previous user.'
1304+ ) ;
12681305 return ;
12691306 }
12701307 inst = activeUser ( ) ;
@@ -1860,6 +1897,9 @@ function initApp() {
18601897 // Actions listeners.
18611898 $ ( '#sign-up-with-email-and-password' ) . click ( onSignUp ) ;
18621899 $ ( '#sign-in-with-email-and-password' ) . click ( onSignInWithEmailAndPassword ) ;
1900+ $ ( '#reauth-with-email-and-password' ) . click (
1901+ onReauthenticateWithEmailAndPassword
1902+ ) ;
18631903 $ ( '.sign-in-with-custom-token' ) . click ( onSignInWithCustomToken ) ;
18641904 $ ( '#sign-in-anonymously' ) . click ( onSignInAnonymously ) ;
18651905 $ ( '#sign-in-with-generic-idp-credential' ) . click (
0 commit comments