According to Firebase documentation (https://firebase.google.com/docs/auth/android/phone-auth#send-a-verification-code-to-the-users-phone), there is callback
for handling the phone number authentication.
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { Log.d(TAG, "onVerificationCompleted:" + credential); signInWithPhoneAuthCredential(credential); } @Override public void onVerificationFailed(FirebaseException e) { Log.w(TAG, "onVerificationFailed", e); } @Override public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) { Log.d(TAG, "onCodeSent:" + verificationId); // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; } };
My question is on onCodeSent
method. It said on the doc here (https://firebase.google.com/docs/reference/android/com/google/firebase/auth/PhoneAuthProvider.ForceResendingToken)
that the token
can be used to force re-sending an SMS verification code. However, after doing some research on the doc I still don't know how.
I would like to ask how to use this token
to resend the SMS verification ?
Record the value of forceResendingToken as resendToken from codeSent callback while sending the otp for first time, then to resend the otp, add one extra parameter forceResendingToken: resendToken in auth. verifyPhoneNumber() function.
Don't forget to go in Firebase Project Settings > App check > and Register firebase project in SafetyNet and Play Integrity register with default time token 1 hour and u will remove reCaptcha from phone auth OTP! That's awesome.
You can use Firebase Authentication to sign in a user by sending an SMS message to the user's phone. The user signs in using a one-time code contained in the SMS message.
Implementing phone number authentication involves sending an SMS to a user's mobile. To do so, we need to pay for SMS services. But via Firebase, we can send an SMS without any cost.
Source: Firebase Quickstarts for Android
This is the method used to resend SMS Verifications.
private void resendVerificationCode(String phoneNumber, PhoneAuthProvider.ForceResendingToken token) { PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNumber, // Phone number to verify 60, // Timeout duration TimeUnit.SECONDS, // Unit of timeout this, // Activity (for callback binding) mCallbacks, // OnVerificationStateChangedCallbacks token); // ForceResendingToken from callbacks }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With