Reputation: 75
I am working for an authentication with phone number in flutter. I am encountering an error with this.
Here is my code:
Future sendOTP() async {
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
print("VERIFICATION completed");
},
verificationFailed: (FirebaseAuthException e) {
print('VERIFICATION ERROR');
},
codeSent: (String verificationId, int? resendToken) async {
print('CODE SENT');
verID = verificationId;
token = resendToken;
},
codeAutoRetrievalTimeout: (String verificationId) {
//print("VERIFICATION timeout");
},
);
}
After sending the OTP to the phone number, and after the user triggers the send button, this method will fire;
void HandleOTP() async {
final _firebaseAuth = FirebaseAuth.instance;
final AuthCredential authCredential = await PhoneAuthProvider.credential(
smsCode: smsCode,
verificationId: verID,
);
print(authCredential);
await _firebaseAuth.signInWithCredential(authCredential);
}
and here is the problem that is printing in the console
I/flutter ( 5939): AuthCredential(providerId: phone, signInMethod: phone, token: null)
E/flutter ( 5939): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.
E/flutter ( 5939): #0 MethodChannelFirebaseAuth.signInWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:445:7)
E/flutter ( 5939): <asynchronous suspension>
E/flutter ( 5939): #1 FirebaseAuth.signInWithCredential (package:firebase_auth/src/firebase_auth.dart:497:7)
E/flutter ( 5939): <asynchronous suspension>
E/flutter ( 5939): #2 _OTPState.authenticate (package:rice_on_the_go/authentication/OTP.dart:215:5)
E/flutter ( 5939): <asynchronous suspension>
As you see, the value of the credential token is NULL, and why is that null? I hope someone help me to answer this.
Second, why is my verification ID is invalid? I checked it and it is not null.
Please help me solving this because I have been working for this for more than a day now. Thank you so much.
Upvotes: 0
Views: 593
Reputation: 374
I make a common function for Firebase PhoneAuth you can try this:
Future<void> callPhoneAuth({required String phoneNumber, required void
Function(String, int?) codeSent, int? forceResendingToken}) async {
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) {
FirebaseAuth.instance.signInWithCredential(credential);
kPrint('phoneauth is an -------- verifynumber ${credential}');
},
codeSent: codeSent,
timeout: const Duration(seconds: 60),
codeAutoRetrievalTimeout: (String verificationId) {},
forceResendingToken: forceResendingToken,
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
kPrint('phoneauth is an ---- The provided phone number is not valid.');
}
});
}
Use like this:
callPhoneAuth(
phoneNumber: '+1123456789', //Here your phone number with country dial code
codeSent: (String verificationId, int? resendToken) {
//This function called when your Otp successfully set it to phone number
});
Upvotes: 1