I am trying to make a loggin with Firebase in Flutter using Phone Auth, I have days going around in a circle with this code, I am handling it using the provide package. Here I left a portion of the code, if you could help me find the error, I would greatly appreciate it. When trying to log in it shows me the following error. The sms code has expired. Please re-send the verification code to try again., Null I still don't fully understand how firebase works with flutter completely, I'm a bit new to this, if someone could help me with this code to see if there are any errors, or just it may be a package failure
_startAuth()async{
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
actualCode = verificationId;
_addStatusMessage("\nEnter the code sent to " + phone);
_addStatus(PhoneAuthState.CodeSent);
// if (onCodeSent != null) onCodeSent();
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
_addStatusMessage('${authException.message}');
_addStatus(PhoneAuthState.Failed);
if (onFailed != null) onFailed();
if (authException.message.contains('not authorized'))
_addStatusMessage('App not authroized');
else if (authException.message.contains('Network'))
_addStatusMessage(
'Please check your internet connection and try again');
else
_addStatusMessage('Something has gone wrong, please try later ' +
authException.message);
};
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential auth) async {
};
_addStatusMessage('Phone auth started');
await FirebaseAuth.instance
.verifyPhoneNumber(
phoneNumber: phone.toString(),
timeout: Duration(seconds: 60),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout)
.then((value) {
if (onCodeSent != null) onCodeSent();
_addStatus(PhoneAuthState.CodeSent);
_addStatusMessage('Code sent');
}).catchError((error) {
if (onError != null) onError();
_addStatus(PhoneAuthState.Error);
_addStatusMessage(error.toString());
});
}
void verifyOTPAndLogin({String smsCode}) async {
smscode = smsCode;
final FirebaseAuth _auth = FirebaseAuth.instance;
_authCredential = PhoneAuthProvider.getCredential(
verificationId: actualCode, smsCode: smsCode);
AuthResult result = await _auth.signInWithCredential(_authCredential);
FirebaseUser user = result.user;
PhoneUser.user = user;
final DocumentSnapshot result1 = await Firestore.instance
.collection('users')
.document(user.uid)
.collection(user.uid)
.document(user.uid)
.get();
if (result1.exists) {
} else {
await UsuarioProvider(uid: user.uid).createUserData(
false,
user.displayName ?? '',
user.email ?? '',
'preferences',
'',
user.uid,
user.photoUrl ?? '',
false,
false
);
}
FirebaseAuth.instance
.signInWithCredential(_authCredential)
.then((AuthResult result) async {
_addStatusMessage('Authentication successful');
_addStatus(PhoneAuthState.Verified);
if (onVerified != null) onVerified();
}).catchError((error) {
if (onError != null) onError();
_addStatus(PhoneAuthState.Error);
_addStatusMessage(
'Something has gone wrong, please try later(signInWithPhoneNumber) $error');
});
}
Future <FirebaseUser> login ()async{
final FirebaseAuth _auth = FirebaseAuth.instance;
_authCredential = PhoneAuthProvider.getCredential(
verificationId: actualCode, smsCode: smscode);
AuthResult result = await _auth.signInWithCredential(_authCredential);
FirebaseUser user = result.user;
// PhoneUser.user = user;
return user;
}