[英]Flutter Firebase Phone Authentication not working
Phone authentication getting failed with the following exception:电话身份验证失败,出现以下异常:
PlatformException(ERROR_SESSION_EXPIRED, The sms code has expired. Please re-send the verification code to try again., null) PlatformException(ERROR_SESSION_EXPIRED, 短信验证码已过期,请重新发送验证码重试。, null)
But it works if I use a different phone number other than that on my phone.但如果我使用的电话号码不同于我手机上的电话号码,它就会起作用。 I've added both SHA-1 and SHA-256 fingerprints from the play store to firebase and also replaced the google-services.json.我已将 Play 商店中的 SHA-1 和 SHA-256 指纹添加到 firebase,还替换了 google-services.json。
Here's my code:这是我的代码:
void _verifyPhoneNumber() async {
setState(() {
isVerified=true;
});
setState(() {
_message = '';
});
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
_auth.signInWithCredential(phoneAuthCredential);
setState(() {
_message = 'Received phone auth credential: $phoneAuthCredential';
});
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
_message =
'Phone number verification failed';
};
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
_verificationId = verificationId;
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
_verificationId = verificationId;
};
await _auth.verifyPhoneNumber(
phoneNumber: '+91'+_phoneNumberController.text,
timeout: const Duration(seconds: 5),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
// Example code of how to sign in with phone.
void _signInWithPhoneNumber() async {
setState(() {
isLoading=true;
});
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: _verificationId,
smsCode: _smsController.text,
);
try{
firebaseUser =
(await _auth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await _auth.currentUser();
assert(firebaseUser.uid == currentUser.uid);
if (firebaseUser != null) {
.....
} else {
_message = 'Sign in failed';
showErrorDialog();
}
}catch (e){
showErrorDialog();
}
setState(() {
isLoading=false;
});
}
not sure of your problem but it says : ERROR_SESSION_EXPIRED, The sms code has expired and in _auth.verifyPhoneNumber()
your Timeout duration is quite low.不确定您的问题,但它说: ERROR_SESSION_EXPIRED,短信代码已过期,在_auth.verifyPhoneNumber()
您的超时持续时间非常低。 try 60 Seconds .尝试60 秒。
await _auth.verifyPhoneNumber(
phoneNumber: '+91${_phoneNumberController.text}',
timeout: Duration(seconds: 60),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
and if this didn't help give a look at the docs .如果这没有帮助,请查看文档。
Right,although timeout property is 60s.是的,虽然超时属性是 60 秒。 But after receiving the verification code and pasting the code immediately, ERROR_SESSION_EXPIRED
like you.但是在收到验证码并立即粘贴验证码后, ERROR_SESSION_EXPIRED
就像您一样。 note: only on Android,just receive on a few phone number.注意:仅在 Android 上,只需接收几个电话号码。
Its late ,but for others who are struggling, it's all is about the incomplete explanation of docs for login and signup.晚了,但对于其他正在挣扎的人来说,这一切都是关于登录和注册文档的不完整解释。
comment the code of signin in _verifyPhoneNumber() function, add async to the function and make AuthCredential to PhoneAuthCredential注释_verifyPhoneNumber() 函数中的登录代码,在函数中添加async 并将AuthCredential 设为PhoneAuthCredential
final PhoneVerificationCompleted verificationCompleted =
(PhoneAuthCredential phoneAuthCredential) async{
// comment the below code for _auth.signIn and add async
// _auth.signInWithCredential(phoneAuthCredential);
setState(() {
_message = 'Received phone auth credential: $phoneAuthCredential';
});
Actually the _verifyPhoneNumber() checks the number in database ,so you can redirect user direct to Home Screen from this function.As the signIn runs two times ,one here and one in signinwithPhone Number() , so it gives timeout error.实际上 _verifyPhoneNumber() 检查数据库中的号码,因此您可以从该函数将用户直接重定向到主屏幕。由于 signIn 运行两次,一次在这里,一次在 signinwithPhone Number() 中,因此它给出了超时错误。
If you missed timeout: Duration(seconds: 60)
in your如果您错过了timeout: Duration(seconds: 60)
_auth.verifyPhoneNumber(
phoneNumber: '+91${_phoneNumberController.text}',
timeout: Duration(seconds: 60),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
This issue may happen这个问题可能会发生
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.