Provider for generating an PhoneAuthCredential.
PhoneAuthProvider
does not work in a Node.js environment.
Signature:
export declare class PhoneAuthProvider
Constructors
Constructor | Modifiers | Description |
---|---|---|
(constructor)(auth) | Constructs a new instance of the PhoneAuthProvider class |
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
PHONE_SIGN_IN_METHOD | static |
'phone' | Always set to SignInMethod.PHONE. |
PROVIDER_ID | static |
'phone' | Always set to ProviderId.PHONE. |
providerId | "phone" | Always set to ProviderId.PHONE. |
Methods
Method | Modifiers | Description |
---|---|---|
credential(verificationId, verificationCode) | static |
Creates 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) | static |
Returns an AuthCredential when passed an error. |
credentialFromResult(userCredential) | static |
Generates 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
Parameter | Type | Description |
---|---|---|
auth | Auth | The Firebase Auth instance in which sign-ins should occur. |
PhoneAuthProvider.PHONE_SIGN_IN_METHOD
Always set to SignInMethod.PHONE.
Signature:
static readonly PHONE_SIGN_IN_METHOD: 'phone';
PhoneAuthProvider.PROVIDER_ID
Always set to ProviderId.PHONE.
Signature:
static readonly PROVIDER_ID: 'phone';
PhoneAuthProvider.providerId
Always set to ProviderId.PHONE.
Signature:
readonly providerId: "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:
static credential(verificationId: string, verificationCode: string): PhoneAuthCredential;
Parameters
Parameter | Type | Description |
---|---|---|
verificationId | string | The verification ID returned from PhoneAuthProvider.verifyPhoneNumber(). |
verificationCode | string | The verification code sent to the user's mobile device. |
Returns:
The auth provider credential.
Example 1
const provider = new PhoneAuthProvider(auth);
const verificationId = provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = signInWithCredential(auth, authCredential);
Example 2
An alternative flow is provided using the signInWithPhoneNumber
method.
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = await confirmationResult.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:
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 as FirebaseError)?.code === 'auth/account-exists-with-different-credential') {
const cred = PhoneAuthProvider.credentialFromError(e);
await linkWithCredential(auth.currentUser, cred);
}
}
// At this point, auth.currentUser.phoneNumber === number.
Signature:
static credentialFromError(error: FirebaseError): AuthCredential | null;
Parameters
Parameter | Type | Description |
---|---|---|
error | FirebaseError | The error to generate a credential from. |
Returns:
AuthCredential | null
PhoneAuthProvider.credentialFromResult()
Generates an AuthCredential from a UserCredential.
Signature:
static credentialFromResult(userCredential: UserCredential): AuthCredential | null;
Parameters
Parameter | Type | Description |
---|---|---|
userCredential | UserCredential | The 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
Parameter | Type | Description |
---|---|---|
phoneOptions | PhoneInfoOptions | string | |
applicationVerifier | ApplicationVerifier | An 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
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, authCredential);
Example 2
An alternative flow is provided using the signInWithPhoneNumber
method.
const confirmationResult = signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = confirmationResult.confirm(verificationCode);
Example
// 'recaptcha-container' is the ID of an element in the DOM.
const applicationVerifier = new RecaptchaVerifier('recaptcha-container');
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber('+16505550101', applicationVerifier);
// Obtain the verificationCode from the user.
const phoneCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, phoneCredential);