Mastering Firebase for Android Development
上QQ阅读APP看书,第一时间看更新

Phone number sign-in 

We have already seen phone number sign-in earlier. This method allows developers to customize the user interface and functionality. The following code makes use of most of the callbacks:

private void setPhoneVerificaton() {
verificationCallbacks =
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(
PhoneAuthCredential credential) {
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
Log.d(TAG, "Invalid credential: "
+ e.getLocalizedMessage());
} else if (e instanceof FirebaseTooManyRequestsException) {
// SMS quota exceeded
Log.d(TAG, "SMS Quota exceeded.");
}
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
phoneVerificationId = verificationId;
resendToken = token;

}
};
}

The important callback methods of phone number sign-in are: 

  • onVerificationCompleted(): This can be triggered when the verification happens automatically without manual input
  • onVerificationFailed(): This callback represents a wrongly entered OTP and other errors
  • onCodeSent(): After code is sent to the number, using this method we can update the UI and other functionalities