PhoneAuthProvider class

Provider for generating an PhoneAuthCredential.

PhoneAuthProvider does not work in a Node.js environment.

Signature:

exportdeclareclassPhoneAuthProvider

Constructors

ConstructorModifiersDescription
(constructor)(auth)Constructs a new instance of the PhoneAuthProvider class

Properties

PropertyModifiersTypeDescription
PHONE_SIGN_IN_METHODstatic'phone'Always set to SignInMethod.PHONE.
PROVIDER_IDstatic'phone'Always set to ProviderId.PHONE.
providerId"phone"Always set to ProviderId.PHONE.

Methods

MethodModifiersDescription
credential(verificationId, verificationCode)staticCreates a phone auth credential, given the verification ID from PhoneAuthProvider.verifyPhoneNumber() and the code that was sent to the user's mobile device.
credentialFromError(error)staticReturns an AuthCredential when passed an error.
credentialFromResult(userCredential)staticGenerates an AuthCredential from a UserCredential.
verifyPhoneNumber(phoneOptions, applicationVerifier)Starts a phone number authentication flow by sending a verification code to the given phone number.

PhoneAuthProvider.(constructor)

Constructs a new instance of the PhoneAuthProvider class

Signature:

constructor(auth:Auth);

Parameters

ParameterTypeDescription
authAuthThe Firebase Auth instance in which sign-ins should occur.

PhoneAuthProvider.PHONE_SIGN_IN_METHOD

Always set to SignInMethod.PHONE.

Signature:

staticreadonlyPHONE_SIGN_IN_METHOD:'phone';

PhoneAuthProvider.PROVIDER_ID

Always set to ProviderId.PHONE.

Signature:

staticreadonlyPROVIDER_ID:'phone';

PhoneAuthProvider.providerId

Always set to ProviderId.PHONE.

Signature:

readonlyproviderId:"phone";

PhoneAuthProvider.credential()

Creates a phone auth credential, given the verification ID from PhoneAuthProvider.verifyPhoneNumber() and the code that was sent to the user's mobile device.

Signature:

staticcredential(verificationId:string,verificationCode:string):PhoneAuthCredential;

Parameters

ParameterTypeDescription
verificationIdstringThe verification ID returned from PhoneAuthProvider.verifyPhoneNumber().
verificationCodestringThe verification code sent to the user's mobile device.

Returns:

PhoneAuthCredential

The auth provider credential.

Example 1

constprovider=newPhoneAuthProvider(auth);constverificationId=provider.verifyPhoneNumber(phoneNumber,applicationVerifier);// Obtain verificationCode from the user.constauthCredential=PhoneAuthProvider.credential(verificationId,verificationCode);constuserCredential=signInWithCredential(auth,authCredential);

Example 2

An alternative flow is provided using the signInWithPhoneNumber method.

constconfirmationResult=awaitsignInWithPhoneNumber(auth,phoneNumber,applicationVerifier);// Obtain verificationCode from the user.constuserCredential=awaitconfirmationResult.confirm(verificationCode);

PhoneAuthProvider.credentialFromError()

Returns an AuthCredential when passed an error.

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:

constprovider=newPhoneAuthProvider(auth);constverificationId=awaitprovider.verifyPhoneNumber(number,verifier);try{constcode='';// Prompt the user for the verification codeawaitupdatePhoneNumber(auth.currentUser,PhoneAuthProvider.credential(verificationId,code));}catch(e){if((easFirebaseError)?.code==='auth/account-exists-with-different-credential'){constcred=PhoneAuthProvider.credentialFromError(e);awaitlinkWithCredential(auth.currentUser,cred);}}// At this point, auth.currentUser.phoneNumber === number.

Signature:

staticcredentialFromError(error:FirebaseError):AuthCredential|null;

Parameters

ParameterTypeDescription
errorFirebaseErrorThe error to generate a credential from.

Returns:

AuthCredential | null

PhoneAuthProvider.credentialFromResult()

Generates an AuthCredential from a UserCredential.

Signature:

staticcredentialFromResult(userCredential:UserCredential):AuthCredential|null;

Parameters

ParameterTypeDescription
userCredentialUserCredentialThe user credential.

Returns:

AuthCredential | null

PhoneAuthProvider.verifyPhoneNumber()

Starts a phone number authentication flow by sending a verification code to the given phone number.

Signature:

verifyPhoneNumber(phoneOptions:PhoneInfoOptions|string,applicationVerifier?:ApplicationVerifier):Promise<string>;

Parameters

ParameterTypeDescription
phoneOptionsPhoneInfoOptions | string
applicationVerifierApplicationVerifierAn ApplicationVerifier, which prevents requests from unauthorized clients. This SDK includes an implementation based on reCAPTCHA v2, RecaptchaVerifier. If you've enabled reCAPTCHA Enterprise bot protection in Enforce mode, this parameter is optional; in all other configurations, the parameter is required.

Returns:

Promise<string>

A Promise for a verification ID that can be passed to PhoneAuthProvider.credential() to identify this flow.

Example 1

constprovider=newPhoneAuthProvider(auth);constverificationId=awaitprovider.verifyPhoneNumber(phoneNumber,applicationVerifier);// Obtain verificationCode from the user.constauthCredential=PhoneAuthProvider.credential(verificationId,verificationCode);constuserCredential=awaitsignInWithCredential(auth,authCredential);

Example 2

An alternative flow is provided using the signInWithPhoneNumber method.

constconfirmationResult=signInWithPhoneNumber(auth,phoneNumber,applicationVerifier);// Obtain verificationCode from the user.constuserCredential=confirmationResult.confirm(verificationCode);

Example

// 'recaptcha-container' is the ID of an element in the DOM.constapplicationVerifier=newRecaptchaVerifier('recaptcha-container');constprovider=newPhoneAuthProvider(auth);constverificationId=awaitprovider.verifyPhoneNumber('+16505550101',applicationVerifier);// Obtain the verificationCode from the user.constphoneCredential=PhoneAuthProvider.credential(verificationId,verificationCode);constuserCredential=awaitsignInWithCredential(auth,phoneCredential);