<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@firebase/auth](./auth.md) &gt; [PhoneAuthProvider](./auth.phoneauthprovider.md) &gt; [credentialFromError](./auth.phoneauthprovider.credentialfromerror.md)

## PhoneAuthProvider.credentialFromError() method

Returns an [AuthCredential](./auth.authcredential.md) when passed an error.

<b>Signature:</b>

```typescript
static credentialFromError(error: FirebaseError): AuthCredential | null;
```

## Parameters

|  Parameter | Type | Description |
|  --- | --- | --- |
|  error | FirebaseError |  |

<b>Returns:</b>

[AuthCredential](./auth.authcredential.md) \| null

## Remarks

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:

```js
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.

```

