public class

PhoneAuthProvider

extends Object
java.lang.Object
   ↳ com.google.firebase.auth.PhoneAuthProvider

Class Overview

Represents the phone number authentication mechanism. Use this class to obtain PhoneAuthCredentials.

Summary

Nested Classes
class PhoneAuthProvider.ForceResendingToken A 'token' that can be used to force re-sending an SMS verification code. 
class PhoneAuthProvider.OnVerificationStateChangedCallbacks Registered callbacks for the different phone auth events. 
Constants
String PROVIDER_ID Unique string identifier for this provider type.
Public Methods
static PhoneAuthCredential getCredential(String verificationId, String smsCode)
Returns a new instance of AuthCredential that is associated with a phone number.
static PhoneAuthProvider getInstance()
Initializes a new PhoneAuthProvider using the default FirebaseAuth instance.
static PhoneAuthProvider getInstance(FirebaseAuth firebaseAuth)
Static method to initialize a new PhoneAuthProvider for the specified Auth instance.
void verifyPhoneNumber(String phoneNumber, long timeout, TimeUnit unit, Activity activity, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks)
Starts the phone number verification process for the given phone number.
void verifyPhoneNumber(String phoneNumber, long timeout, TimeUnit unit, Executor executor, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks)
void verifyPhoneNumber(String phoneNumber, long timeout, TimeUnit unit, Activity activity, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks, PhoneAuthProvider.ForceResendingToken forceResendingToken)
void verifyPhoneNumber(String phoneNumber, long timeout, TimeUnit unit, Executor executor, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks, PhoneAuthProvider.ForceResendingToken forceResendingToken)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String PROVIDER_ID

Unique string identifier for this provider type.

Constant Value: "phone"

Public Methods

public static PhoneAuthCredential getCredential (String verificationId, String smsCode)

Returns a new instance of AuthCredential that is associated with a phone number. Used when calling signInWithCredential(AuthCredential) or linkWithCredential(AuthCredential).

Parameters
verificationId String: a valid verificationId retrieved by calling verifyPhoneNumber(String, long, TimeUnit, Activity, OnVerificationStateChangedCallbacks)
smsCode String: the 6 digit SMS-code sent to the user
Returns
PhoneAuthCredential

public static PhoneAuthProvider getInstance ()

Initializes a new PhoneAuthProvider using the default FirebaseAuth instance.

Returns
PhoneAuthProvider

public static PhoneAuthProvider getInstance (FirebaseAuth firebaseAuth)

Static method to initialize a new PhoneAuthProvider for the specified Auth instance.

Parameters
firebaseAuth FirebaseAuth
Returns
PhoneAuthProvider

public void verifyPhoneNumber (String phoneNumber, long timeout, TimeUnit unit, Activity activity, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks)

Starts the phone number verification process for the given phone number. Either sends an SMS with a 6 digit code to the phone number specified or triggers the callback with a complete AuthCredential that can be used to log in the user.

The specified callback will be Activity-scoped, i.e. it will be automatically removed during onStop(). This function is reentrant and can be called again in onStart(). No duplicated SMS will be sent out upon re-entry (before timeout).

Make sure to test all scenarios below:

  • You directly get back an AuthCredential if Google Play services verified the phone number instantly or helped you auto-retrieve the verification code.
  • auto-retrieve verification code timed out.
  • error cases when you receive onVerificationFailed(FirebaseException).

Parameters
phoneNumber String: the phone number for the account the user is signing up for or signing into. Make sure to pass in a phone number with country code prefixed with plus sign ('+').
timeout long: the maximum amount of time you are willing to wait for SMS auto-retrieval to be completed by the library. Maximum allowed value is 2 minutes. Use 0 to disable SMS-auto-retrieval. If you specifies a positive value less than 30 seconds, library will default to 30 seconds.
unit TimeUnit: the TimeUnit for the timeout
activity Activity: the Activity to which the callbacks are scoped.
callbacks PhoneAuthProvider.OnVerificationStateChangedCallbacks: the callbacks to get the status of phone number verification. The callbacks will be automatically removed when the specified activity has stopped.

public void verifyPhoneNumber (String phoneNumber, long timeout, TimeUnit unit, Executor executor, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks)

See verifyPhoneNumber(String, long, TimeUnit, Activity, OnVerificationStateChangedCallbacks) for details. This overload allows specifying an Executor for the callbacks to be called on.

If your app is handling UI transitions in the callbacks, we recommend verifyPhoneNumber(String, long, TimeUnit, Activity, OnVerificationStateChangedCallbacks) which can call back your app on main thread and auto-detach callbacks when Activity stops.

If you do not want your callbacks be scoped to Activity, but still desire callbacks on main thread, you can use this method with MAIN_THREAD.

If you want callbacks on a different thread, pass corresponding Executor to this method.

Parameters
phoneNumber String
timeout long
unit TimeUnit
executor Executor: the Executor used to call the OnVerificationStateChangedCallbacks callbacks
callbacks PhoneAuthProvider.OnVerificationStateChangedCallbacks

public void verifyPhoneNumber (String phoneNumber, long timeout, TimeUnit unit, Activity activity, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks, PhoneAuthProvider.ForceResendingToken forceResendingToken)

See verifyPhoneNumber(String, long, TimeUnit, Activity, OnVerificationStateChangedCallbacks) for details. This overload allows specifying a PhoneAuthProvider.ForceResendingToken.

Parameters
phoneNumber String
timeout long
unit TimeUnit
activity Activity
callbacks PhoneAuthProvider.OnVerificationStateChangedCallbacks
forceResendingToken PhoneAuthProvider.ForceResendingToken: the ForceResendingToken obtained from onCodeSent(String, PhoneAuthProvider.ForceResendingToken) callback to force re-sending another verification SMS before the auto-retrieval timeout.

public void verifyPhoneNumber (String phoneNumber, long timeout, TimeUnit unit, Executor executor, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks, PhoneAuthProvider.ForceResendingToken forceResendingToken)

See verifyPhoneNumber(String, long, TimeUnit, Executor, OnVerificationStateChangedCallbacks) for details. This overload allows specifying an Executor for the callbacks to be called on and a PhoneAuthProvider.ForceResendingToken.

Parameters
phoneNumber String
timeout long
unit TimeUnit
executor Executor: the executor used to call the OnVerificationStateChangedCallbacks callbacks
callbacks PhoneAuthProvider.OnVerificationStateChangedCallbacks
forceResendingToken PhoneAuthProvider.ForceResendingToken: the ForceResendingToken obtained from onCodeSent(String, PhoneAuthProvider.ForceResendingToken) callback to force re-sending another verification SMS before the auto-retrieval timeout.