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 verifyingverificationCompleted
: 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 anAuthCredential
from it with the providedverificationId
and the sms code.
Comments
-
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 almost 3 yearsThis is not the case !!! VerificationId is created whenever the request to authentication is made. And it is uniques every time
-
Rajendra A Verma almost 3 years@AliYarKhan how to avoid this error ? any help
-
Ali Yar Khan almost 3 years@RajendraAVerma i haven't found any solution :-/
-
Kyaw San Oo over 2 yearsMake 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.