Reauthenticating users

Certain sensitive operations—such as deleting an account, updating a user's email, changing a password, or enabling multi-factor authentication—require the user to have signed in recently. If you perform one of these actions, and the user signed in too long ago, the action fails with an error. Depending on the use case, this can be done with reauthenticateWithPopup(), reauthenticateWithRedirect() or reauthenticateWithCredential().

Example: Reauthenticate Apple Sign-in with Popup

Web version 9

import{getAuth,reauthenticateWithPopup,OAuthProvider}from"firebase/auth";
// Result from Redirect auth flow.
constauth=getAuth();
constprovider=newOAuthProvider('apple.com');
reauthenticateWithPopup(auth.currentUser,provider)
.then((result)=>{
// User is re-authenticated with fresh tokens minted and can perform
// sensitive operations like account deletion, or updating their email
// address or password.
// The signed-in user info.
constuser=result.user;
// You can also get the Apple OAuth Access and ID Tokens.
constcredential=OAuthProvider.credentialFromResult(result);
constaccessToken=credential.accessToken;
constidToken=credential.idToken;
// ...
})
.catch((error)=>{
// Handle Errors here.
consterrorCode=error.code;
consterrorMessage=error.message;
// The email of the user's account used.
constemail=error.customData.email;
// The credential that was used.
constcredential=OAuthProvider.credentialFromError(error);
// ...
});

Web version 8

constprovider=newfirebase.auth.OAuthProvider('apple.com');
firebase
.auth()
.currentUser
.reauthenticateWithPopup(provider)
.then((result)=>{
// User is re-authenticated with fresh tokens minted and can perform
// sensitive operations like account deletion, or updating their email
// address or password.
/** @type {firebase.auth.OAuthCredential} */
varcredential=result.credential;
// The signed-in user info.
varuser=result.user;
// You can also get the Apple OAuth Access and ID Tokens.
varaccessToken=credential.accessToken;
varidToken=credential.idToken;
// IdP data available in result.additionalUserInfo.profile.
// ...
})
.catch((error)=>{
// Handle Errors here.
varerrorCode=error.code;
varerrorMessage=error.message;
// The email of the user's account used.
varemail=error.email;
// The firebase.auth.AuthCredential type that was used.
varcredential=error.credential;
// ...
});

Example: reauthenticateWithCredential()

Web version 9

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

Web version 8

constuser=firebase.auth().currentUser;
// TODO(you): prompt the user to re-provide their sign-in credentials
constcredential=promptForCredentials();
user.reauthenticateWithCredential(credential).then(()=>{
// User re-authenticated.
}).catch((error)=>{
// An error occurred
// ...
});

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月30日 UTC.