我正在尝试在Flutter应用程序中实现Firebase电话身份验证,我使用了以下软件包:Firebase_core和Firebase_auth,这是我使用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';
});
},
);
但是我总是会收到此错误:
[firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.
有帮助吗?
答案 0 :(得分:1)
我不确定为什么存在此行:
var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
您已经从参数authCredential
中获得了凭据,因此请使用该凭据登录,因为它是有效的。
verifyPhoneNumber
方法在FirebaseAuth
中的工作方式如下:
verificationFailed
:回调验证时抛出的所有错误verificationCompleted
:如果不需要验证短信(Google知道它是有效的电话号码)codeSent
:发送代码时触发,此功能是您让用户输入短信代码并使用提供的AuthCredential
和代码创建verificationId
的功能短信代码。