Home > Blockchain >  How to stop an ongoing phone authentication request with Firebase
How to stop an ongoing phone authentication request with Firebase

Time:12-04

I have added to my phone authentication to my sign up process, in a send code activity - which sends the sms code to confirm the phone authentication process. Then, I have also added a "go-back"/"return" button which moves the user back to the main activity.

If I make the following request which sends the user a sms code to his phone:

PhoneAuthProvider.verifyPhoneNumber(options);

I won't be able to make another request before the defined timeout duration ends. Therefore, I thought about the easy and not messy approach, that would be to cancel the ongoing request, but unfortunately couldn't find how to do so, if even possible nowadays. I have also saw the unanswered post here: Android Firebase OTP auth: Is there a way to cancel OTP code request programatically before the actual timeout?

Couldn't work with this, even though it's what I am looking for, but it has no related answers.

  • Note: I am programming my project with Java and not Kotlin.

I have also thought about the second approach, which is to save current activity's phone number and then extract it with onRestoreInstanceState and onSaveInstanceState, then resend a code sms again. But of course, it's much more complicated and messier.

CodePudding user response:

To cancel an ongoing phone verification request in Firebase, you can use the PhoneAuthProvider.ForceResendingToken class. This class represents a token that can be used to force the resending of a verification code.

To cancel an ongoing phone verification request, you can use the PhoneAuthProvider.verifyPhoneNumber method to initiate the request, and then call the PhoneAuthProvider.verifyPhoneNumber method again with the same phoneNumber and ForceResendingToken arguments. This will cancel the previous request and start a new one, allowing you to resend the verification code.

Here is an example of how you can cancel an ongoing phone verification request in Firebase:

// Initiate the phone verification request
PhoneAuthProvider.verifyPhoneNumber(
  phoneNumber,
  timeoutDuration,
  activity,
  new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    // Handle the verification state change events
    @Override
    public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) {
      // Save the verification ID and token
      this.verificationId = verificationId;
      this.token = token;
    }
    // ...
  }
);

// Cancel the ongoing phone verification request
PhoneAuthProvider.verifyPhoneNumber(
  phoneNumber,
  timeoutDuration,
  activity,
  new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    // Handle the verification state change events
    @Override
    public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) {
      // Save the verification ID and token
      this.verificationId = verificationId;
      this.token = token;
    }
    // ...
  },
  token //

CodePudding user response:

It is possible to cancel an ongoing verification request by calling the verifyPhoneNumber method again with the same phone number, but with the forceResendingToken parameter set to null. This will cancel the previous request and allow to start a new one.

It is also possible to use the PhoneAuthProvider.getInstance() method to get a reference to the PhoneAuthProvider instance, and then call the verifyPhoneNumber method on that instance instead of calling it directly. This allows to call the verifyPhoneNumber method multiple times without canceling the previous request.

Timeout duration for verification requests is typically around 5 minutes, so if you want to allow the user to request a new code before the timeout expires, provide a way for them to do so, such as by adding a "Resend code" button to the app.

Overall, it's best to design apps in a way that minimizes the need for canceling ongoing verification requests, as this can lead to a confusing user experience. Instead, focus on providing clear instructions and options for the user, and consider using the getInstance method to avoid having to cancel requests altogether.

  • Related