我通过firebase phone auth进行身份验证,在Android上,我想通过" verificationcomplete "自动进行身份验证。然而,函数"codeSent">
Future<void> _sendingCode(String phoneNum, BuildContext context) async {
FirebaseAuth auth = FirebaseAuth.instance;
return await auth.verifyPhoneNumber(
phoneNumber: phoneNum,
forceResendingToken: _forceResendingToken,
verificationCompleted: (PhoneAuthCredential credential) async {
logger.d('verificationCompleted - $credential');
// Sign the user in (or link) with the auto-generated credential
await auth.signInWithCredential(credential).then((value) async {
// next page
context.read<PageController>().animateToPage(2,
duration: Duration(milliseconds: 500), curve: Curves.ease);
}).catchError((e) => logger.w(e));
},
codeAutoRetrievalTimeout: (String verificationId) {
logger.w('timeout');
},
codeSent: (String verificationId, int? forceResendingToken) {
logger.d("$phoneNum send code");
_verificationId = verificationId;
_forceResendingToken = forceResendingToken;
},
verificationFailed: (FirebaseAuthException error) {
logger.e("Error: $error");
},
);
}
我能知道这个问题的原因吗?
onVerificationCompleted()
仅在验证电话号码时才会被调用,而无需用户输入任何内容。要做你正在尝试的事情,你应该在onCodeSent()
中发送你的动作。
从用户
获取电话号码呼叫
PhoneAuthProvider.verifyPhoneNumber(auth)
(你已经)发送引脚给用户 调用onCodeSent()
,带有验证ID和重发令牌。在
onCodeSent()
内部,创建一个方法来启动"引脚输入屏幕";验证ID。从用户那里获得一个pin,然后通过调用
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, userInput)
将其与验证ID相结合使用该凭据使用
signInWithCredential(credential).
登录用户
您必须实现这两个功能。onVerificationCompleted
功能一直不工作
onVerificationCompleted
的详细信息link
在您的codeSent
方法中添加相同的逻辑。