Reputation: 123
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.
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.
Upvotes: 2
Views: 983
Reputation: 519
I'm using com.google.firebase:firebase-bom:32.0.0
where PhoneAuthProvider.getInstance()
is deprecated and cannot set setForceResendingToken(null)
.
When I was checking the source definition for ForceResendingToken
I found a public method zza()
returning new ForceResendingToken
instance. So I tried using it setForceResendingToken(PhoneAuthProvider.ForceResendingToken.zza())
and it worked like a charm.
Currently I'm using this when I don't have a valid PhoneAuthProvider.ForceResendingToken
object.
I'm not sure this is the right way but it gets the job done without any issues for now.
Upvotes: 1
Reputation: 7815
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.
Upvotes: 3