voluptasmollitia
Version:
Monorepo for the Firebase JavaScript SDK
53 lines (34 loc) • 2.01 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[](./index.md) > [@firebase/auth](./auth.md) > [MultiFactorUser](./auth.multifactoruser.md) > [enroll](./auth.multifactoruser.enroll.md)
Enrolls a second factor as identified by the [MultiFactorAssertion](./auth.multifactorassertion.md) for the user.
<b>Signature:</b>
```typescript
enroll(assertion: MultiFactorAssertion, displayName?: string | null): Promise<void>;
```
| Parameter | Type | Description |
| --- | --- | --- |
| assertion | [MultiFactorAssertion](./auth.multifactorassertion.md) | The multi-factor assertion to enroll with. |
| displayName | string \| null | The display name of the second factor. |
<b>Returns:</b>
Promise<void>
On resolution, the user tokens are updated to reflect the change in the JWT payload. Accepts an additional display name parameter used to identify the second factor to the end user. Recent re-authentication is required for this operation to succeed. On successful enrollment, existing Firebase sessions (refresh tokens) are revoked. When a new factor is enrolled, an email notification is sent to the user’s email.
## Example
```javascript
const multiFactorUser = multiFactor(auth.currentUser);
const multiFactorSession = await multiFactorUser.getSession();
// Send verification code.
const phoneAuthProvider = new PhoneAuthProvider(auth);
const phoneInfoOptions = {
phoneNumber: phoneNumber,
session: multiFactorSession
};
const verificationId = await phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, appVerifier);
// Obtain verification code from user.
const phoneAuthCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(phoneAuthCredential);
await multiFactorUser.enroll(multiFactorAssertion);
// Second factor enrolled.
```