如何使Firebase OTP返回SMS代码,以便将其用于验证以外的其他验证?
以下Firebase功能将OTP代码发送到电话号码。 问题是.. _onverificationcompleted
仅在发送请求的设备上的电话号码(SIM卡)时才调用。 如果我需要知道_onverificationcompleted
方法将永远不会被调用时,我需要知道已发送的OTP代码怎么办? 我尝试了phoneAuthProvider.credential(verificationId:验证:smscode:enteredCode);
> 但是,此方法的返回类型是PhoneAuthcredential
,仅在尝试签名/登录用户时才使用。
我只需要验证输入的代码== smscode而不签名即可。 任何帮助都将不胜感激!
Future<void> verifyPhoneNumber({@required String phoneNumber}) async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: _onVerificationCompleted,
verificationFailed: _onVerificationFailed,
codeSent: _onCodeSent,
codeAutoRetrievalTimeout: _onCodeTimeout);
}
_onVerificationCompleted(PhoneAuthCredential authCredential) async {
print("verification completed ${authCredential.smsCode}");
if (otpTextFieldController.text == authCredential.smsCode) {
print('Verified!');
} else {
print('not-verified!');
}
setState(() {
_numberValidated = true;
});
if (authCredential.smsCode != null) {
try {} on FirebaseAuthException catch (e) {
print('OTP error : $e');
}
setState(() {
isLoading = false;
});
}
}
_onVerificationFailed(FirebaseAuthException exception) {
print('verification : ${exception}');
if (exception.code == 'invalid-phone-number') {
showMessage("The phone number entered is invalid!");
}
}
_onCodeSent(String verificationId, int forceResendingToken) {
this.verificationId = verificationId;
print(forceResendingToken);
print("code sent = ${this.verificationId}");
}
_onCodeTimeout(String timeout) {
return null;
}
void showMessage(String errorMessage) {
showDialog(
context: context,
builder: (BuildContext builderContext) {
return AlertDialog(
title: Text("Error"),
content: Text(errorMessage),
actions: [
TextButton(
child: Text("Ok"),
onPressed: () async {
Navigator.of(builderContext).pop();
},
)
],
);
}).then((value) {
setState(() {
isLoading = false;
});
});
}
The following firebase function sends the OTP code to the phone number.
Problem is.. _onVerificationCompleted
gets only invoked when the phone number is on the device that is sending the request (SIM card).
What if I need to know the sent otp code when the _onVerificationCompleted
method will never get called as it's a different device phone number?
I tried PhoneAuthProvider.credential(verificationId: verification, smsCode: enteredCode);
But this method's return type is PhoneAuthCredential
, which is only used when trying to signin/login the user.
I just need to verify the entered code == the smsCode without signing in.
Any help would be really appreciated!
Future<void> verifyPhoneNumber({@required String phoneNumber}) async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: _onVerificationCompleted,
verificationFailed: _onVerificationFailed,
codeSent: _onCodeSent,
codeAutoRetrievalTimeout: _onCodeTimeout);
}
_onVerificationCompleted(PhoneAuthCredential authCredential) async {
print("verification completed ${authCredential.smsCode}");
if (otpTextFieldController.text == authCredential.smsCode) {
print('Verified!');
} else {
print('not-verified!');
}
setState(() {
_numberValidated = true;
});
if (authCredential.smsCode != null) {
try {} on FirebaseAuthException catch (e) {
print('OTP error : $e');
}
setState(() {
isLoading = false;
});
}
}
_onVerificationFailed(FirebaseAuthException exception) {
print('verification : ${exception}');
if (exception.code == 'invalid-phone-number') {
showMessage("The phone number entered is invalid!");
}
}
_onCodeSent(String verificationId, int forceResendingToken) {
this.verificationId = verificationId;
print(forceResendingToken);
print("code sent = ${this.verificationId}");
}
_onCodeTimeout(String timeout) {
return null;
}
void showMessage(String errorMessage) {
showDialog(
context: context,
builder: (BuildContext builderContext) {
return AlertDialog(
title: Text("Error"),
content: Text(errorMessage),
actions: [
TextButton(
child: Text("Ok"),
onPressed: () async {
Navigator.of(builderContext).pop();
},
)
],
);
}).then((value) {
setState(() {
isLoading = false;
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论