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

3,599

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.
Share:
3,599
Ahmed Wagdi
Author by

Ahmed Wagdi

A newcomer to the pythonic world

Updated on December 23, 2022

Comments

  • Ahmed Wagdi
    Ahmed Wagdi over 1 year

    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 ??

  • Ali Yar Khan
    Ali Yar Khan almost 3 years
    This is not the case !!! VerificationId is created whenever the request to authentication is made. And it is uniques every time
  • Rajendra A Verma
    Rajendra A Verma almost 3 years
    @AliYarKhan how to avoid this error ? any help
  • Ali Yar Khan
    Ali Yar Khan almost 3 years
    @RajendraAVerma i haven't found any solution :-/
  • Kyaw San Oo
    Kyaw San Oo over 2 years
    Make sure you passed the real verificationId value while your app is on background when reading sms code. In my case, I make verificationId as global variable after codeSent callback as _verificationId and use it in signInWithSmsCode(String smsCode) as follows. AuthCredential authCredential = PhoneAuthProvider.credential(smsCode: smsCode, verificationId: _verificationId); and it is working.