Home > @firebase/auth > MultiFactorUser > enroll
MultiFactorUser.enroll() method
Enrolls a second factor as identified by the MultiFactorAssertion for the user.
Signature:
enroll(assertion: MultiFactorAssertion, displayName?: string | null): Promise<void>;
Parameters
Parameter | Type | Description |
---|---|---|
assertion | MultiFactorAssertion | The multi-factor assertion to enroll with. |
displayName | string | null | The display name of the second factor. |
Returns:
Promise<void>
Description
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
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.