Home > @firebase/auth > PhoneAuthProvider > credentialFromError
PhoneAuthProvider.credentialFromError() method
Returns an AuthCredential when passed an error.
Signature:
static credentialFromError(error: FirebaseError): AuthCredential | null;
Parameters
Parameter | Type | Description |
---|---|---|
error | FirebaseError |
Returns:
AuthCredential | null
Description
This method works for errors like auth/account-exists-with-different-credentials
. This is useful for recovering when attempting to set a user's phone number but the number in question is already tied to another account. For example, the following code tries to update the current user's phone number, and if that fails, links the user with the account associated with that number:
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(number, verifier);
try {
const code = ''; // Prompt the user for the verification code
await updatePhoneNumber(
auth.currentUser,
PhoneAuthProvider.credential(verificationId, code));
} catch (e) {
if (e.code === 'auth/account-exists-with-different-credential') {
const cred = PhoneAuthProvider.credentialFromError(e);
await linkWithCredential(auth.currentUser, cred);
}
}
// At this point, auth.currentUser.phoneNumber === number.