简体   繁体   中英

The verification ID used to create the phone auth credential is invalid in flutter

I'm trying to achieve Firebase phone auth in my flutter application, I've used packages : Firebase_core And Firebase_auth , here is the code i've use d:

          FirebaseAuth _auth = FirebaseAuth.instance;
          _auth.verifyPhoneNumber(
              phoneNumber: '+2$mobile',
              timeout: Duration(seconds: 60),
              verificationCompleted: (AuthCredential authCredential){
                var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
                _auth.signInWithCredential(_credential).then((UserCredential result) async {
                  pr.hide();
                  setState(() {
                    status = 'Authentication successful';
                  });
//The rest of my success code
                }).catchError((e){
                  print(e);
                  Navigator.of(context).pushAndRemoveUntil(
                      MaterialPageRoute(
                          builder: (context) => Welcome()),
                          (Route<dynamic> route) => false);                
};
              },
              verificationFailed: (FirebaseAuthException  authException){
                print(authException.message);
              },
              codeSent: (String verificationId, [int forceResendingToken]){
                setState(() {
                  actualCode = verificationId;
                  status = 'Code sent';
                });
              },
              codeAutoRetrievalTimeout: (String verificationId){
                verificationId = verificationId;
                print(verificationId);
                setState(() {
                  status = 'Auto retrieval timeout';
                });
              },
              );

But i always get this error :

[firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.

Any help ??

I'm not sure why this line exists:

var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);

You already get a credential from the parameter authCredential so just sign in with that since it's valid.

The way the verifyPhoneNumber method works in FirebaseAuth is as follows:

  • verificationFailed : callback for any errors thrown while verifying
  • verificationCompleted : if no sms code was needed to verify (google knows it's a valid number)
  • codeSent : triggered when a code was sent, this is the function that you would have the user enter the sms code and create an AuthCredential from it with the provided verificationId and the sms code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM