flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

Resending SMS using "forceResendingToken" on verifyPhoneNumber

Open anraiki opened this issue 6 years ago • 7 comments

I can't seem to figure out how to resend a SMS if the first one was lost or not received.

I dug into verifyPhoneNumber and found forceResendingToken but I couldn't find any documentation on how to use or apply it.

Some guidance would be much apprecited.

  /// [forceResendingToken] The [forceResendingToken] obtained from [codeSent]
  ///   callback to force re-sending another verification SMS before the
  ///   auto-retrieval timeout.

anraiki avatar Mar 23 '20 10:03 anraiki

Same issue here. Been trying to figure it out for almost 5 hours now.

JemZubiri-Projects avatar Mar 23 '20 11:03 JemZubiri-Projects

Hi @anraiki @JemZubiri-Projects Please check this Stackoverflow answer and you can find PhoneCodeSent api

Hope it helps Thanks

TahaTesser avatar Mar 23 '20 12:03 TahaTesser

@TahaTesser Hi Taha, that answer and solution leads back to this: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/verifyPhoneNumber.html

Which still does not explain properly how to use forceResendingToken.

The PhoneCodeSent executes after an SMS is sent but I am trying to do a manual resend in case the user finds that they have not received their code.

anraiki avatar Mar 23 '20 16:03 anraiki

Need this ASAP. Please provide an explanation on how to use it because that was our problem.

JemZubiri-Projects avatar Mar 24 '20 02:03 JemZubiri-Projects

It's time for discovery!

I believe our next step is to hold onto the forceResendingToken and drop it into verifyPhoneNumber and call it again.

This should essentially rerun the function again and send the sms once more and loop back to the verification screen.

anraiki avatar Mar 24 '20 07:03 anraiki

Tried that and there are times that the OTP is not resending. Maybe because the timeout is not yet called?

If yours is working stably, can you please show me how you did yours? Thank you

JemZubiri-Projects avatar Mar 24 '20 07:03 JemZubiri-Projects

Seems to be working for me. It sent the same SMS code 3x from the same text services.

await _auth.verifyPhoneNumber(
                          phoneNumber: "+1$truePhoneNumber",
                          timeout: const Duration(seconds: 5),
                          forceResendingToken: _forceResendingToken,
                          verificationCompleted: (AuthCredential phoneAuthCredential) async {
                            await _auth.signInWithCredential(phoneAuthCredential);
                            final FirebaseUser currentUser = await _auth.currentUser();
                            if( currentUser.uid.isNotEmpty ) {
                              await _pushSignIn(currentUser);
                            } else {
                              print("Could not get Current User");
                            }
                          },
                          verificationFailed: (AuthException authException) {
                            print('Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
                            _alertError('There was an error in sending the code. Please contact us to help you sort this issue.');
                            return;
                          },
                          codeSent: (String verificationId, [int forceResendingToken]) async {
                            setState(() {
                              _loading = false;
                            });
                            _verificationId = verificationId;
                            _forceResendingToken = forceResendingToken;
                          },
                          codeAutoRetrievalTimeout: (String verificationId) {
                            // print("Time Out. Duration Exceeded");
                            _verificationId = verificationId;
                          }
                        );

anraiki avatar Mar 24 '20 08:03 anraiki