Search code examples
javascriptfirebasefirebase-authentication

Firebase 'requires-recent-login'


I would like to know about firebase requires-recent-login. If I reauthenticate user like this

firebase.auth().currentUser.reauthenticateAndRetrieveDataWithCredential(
  firebase.auth.EmailAuthProvider.credential(firebase.auth().currentUser.email,
  typedpassword))

to let user change password or email, is there any possibility that requires-recent-login error comes out?


Solution

  • Some actions require recent authentication and it fails with a auth/requires-recent-login error code.

    One action that requires recent authentication is the updating of email.

    The solution is to reauthenticate the user, before retrying the action.

    import { getAuth, reauthenticateWithCredential } from 'firebase/auth';
    
    const auth = getAuth();
    const user = auth.currentUser;
    
    // TODO(you): prompt the user to re-provide their sign-in credentials
    const credential = promptForCredentials();
    
    reauthenticateWithCredential(user, credential).then(() => {
      // User re-authenticated.
    }).catch((error) => {
      // An error ocurred
      // ...
    });