firebase:: auth:: PhoneAuthProvider
#include <credential.h>
Use phone number text messages to authenticate.
Summary
Allows developers to use the phone number and SMS verification codes to authenticate a user on a mobile device.
This class is not supported on tvOS and Desktop platforms.
The verification flow results in aCredential that can be used to,
- Sign in to an existing phone number account/sign up with a new phone number
- Link a phone number to a current user. This provider will be added to the user.
- Update a phone number on an existing user.
- Re-authenticate an existing user. This may be needed when a sensitive operation requires the user to be recently logged in.
Possible verification flows: (1)User manually enters verification code.
- App callsVerifyPhoneNumber.
- Web verification page is displayed to user where they may need to solve a CAPTCHA. [iOS only].
- Auth server sends the verification code via SMS to the provided phone number.App receives verification id viaListener::OnCodeSent().
- User receives SMS and enters verification code in app's GUI.
- App uses user's verification code to callPhoneAuthProvider::GetCredential.
(2) SMS is automatically retrieved (Android only).
- App callsVerifyPhoneNumber with
timeout_ms> 0. - Auth server sends the verification code via SMS to the provided phone number.
- SMS arrives and is automatically retrieved by the operating system.Credential is automatically created and passed to the app viaListener::OnVerificationCompleted().
(3) Phone number is instantly verified (Android only).
- App callsVerifyPhoneNumber.
- The operating system validates the phone number without having to send an SMS.Credential is automatically created and passed to the app viaListener::OnVerificationCompleted().
All three flows can be handled with the example code below. The flow is complete when PhoneVerifier::credential() returns non-NULL.
classPhoneVerifier:publicPhoneAuthProvider::Listener{public:PhoneVerifier(constchar*phone_number,PhoneAuthProvider*phone_auth_provider):display_message_("Sending SMS with verification code"),display_verification_code_input_box_(false),display_resend_sms_button_(false),phone_auth_provider_(phone_auth_provider),phone_number_(phone_number){SendSms();}~PhoneVerifier()override{}voidOnVerificationCompleted(Credentialcredential)override{// Grab `mutex_` for the scope of `lock`. Callbacks can be called on// other threads, so this mutex ensures data access is atomic.MutexLocklock(mutex_);credential_=credential;}voidOnVerificationFailed(conststd::string&error)override{MutexLocklock(mutex_);display_message_="Verification failed with error: "+error;}voidOnCodeSent(conststd::string&verification_id,constPhoneAuthProvider::ForceResendingToken&force_resending_token)override{MutexLocklock(mutex_);verification_id_=verification_id;force_resending_token_=force_resending_token;display_verification_code_input_box_=true;display_message_="Waiting for SMS";}voidOnCodeAutoRetrievalTimeOut(conststd::string&verification_id)override{MutexLocklock(mutex_);display_resend_sms_button_=true;}// Draw the verification GUI on screen and process input events.voidDraw(){MutexLocklock(mutex_);// Draw an informative message describing what's currently happening.ShowTextBox(display_message_.c_str());// Once the time out expires, display a button to resend the SMS.// If the button is pressed, call VerifyPhoneNumber again using the// force_resending_token_.if(display_resend_sms_button_&&!verification_id_.empty()){constboolresend_sms=ShowTextButton("Resend SMS");if(resend_sms){SendSms();}}// Once the SMS has been sent, allow the user to enter the SMS// verification code into a text box. When the user has completed// entering it, call GetCredential() to complete the flow.if(display_verification_code_input_box_){conststd::stringverification_code=ShowInputBox("Verification code");if(!verification_code.empty()){credential_=phone_auth_provider_->GetCredential(verification_id_.c_str(),verification_code.c_str());}}}// The phone number verification flow is complete when this returns// non-NULL.Credential*credential(){MutexLocklock(mutex_);returncredential_.is_valid()?&credential_:nullptr;}private:voidSendSms(){staticconstuint32_tkAutoVerifyTimeOut=2000;MutexLocklock(mutex_);phone_auth_provider_->VerifyPhoneNumber(phone_number_.c_str(),kAutoVerifyTimeOut,&force_resending_token_,this);display_resend_sms_button_=false;}// GUI-related variables.std::stringdisplay_message_;booldisplay_verification_code_input_box_;booldisplay_resend_sms_button_;// Phone flow related variables.PhoneAuthProvider*phone_auth_provider_;std::stringphone_number_;std::stringverification_id_;PhoneAuthProvider::ForceResendingTokenforce_resending_token_;Credentialcredential_;// Callbacks can be called on other threads, so guard them with a mutex.Mutexmutex_;};
Public static attributes | |
|---|---|
kProviderId | const char *constThe string used to identify this provider. |
Public functions | |
|---|---|
GetCredential(const char *verification_id, const char *verification_code) | Generate a credential for the given phone number. |
VerifyPhoneNumber(constPhoneAuthOptions & options,PhoneAuthProvider::Listener *listener) | voidStart the phone number authentication operation. |
Public static functions | |
|---|---|
GetInstance(Auth *auth) | Return thePhoneAuthProvider for the specified auth. |
Classes | |
|---|---|
| firebase:: | Token to maintain current phone number verification session. |
| firebase:: | Receive callbacks fromVerifyPhoneNumber events. |
Public static attributes
kProviderId
constchar*constkProviderId
The string used to identify this provider.
Public functions
GetCredential
PhoneAuthCredentialGetCredential(constchar*verification_id,constchar*verification_code)
Generate a credential for the given phone number.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
| ||||
| Returns | NewCredential. |
VerifyPhoneNumber
voidVerifyPhoneNumber(constPhoneAuthOptions&options,PhoneAuthProvider::Listener*listener)
Start the phone number authentication operation.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
Public static functions
GetInstance
PhoneAuthProvider&GetInstance(Auth*auth)
Return thePhoneAuthProvider for the specifiedauth.
| Details | |||
|---|---|---|---|
| Parameters |
|
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-05-07 UTC.