javascript - 使用firebase从react native发送OTP时面临问题
问题描述
我正在尝试使用 firebase 从 react-native 发送 OTP,但它不起作用。我收到错误“未找到主机名”。我想了解这种发送 OTP 如何在使用 firebase 的移动应用程序中工作。我可以通过仅安装 .apk 而不是将 .apk 上传到 Play 商店来测试移动/本地中的 OTP 功能吗?
我做了以下配置步骤:
- 在 firebase 控制台中创建项目。
- 在身份验证下选择手机作为登录方法。
- 添加了移动应用程序并下载了 google-services.json 并在我的代码中的 firebase 属性中使用了相同的配置。
发送一次性密码
const phoneProvider = new firebase.auth.PhoneAuthProvider();
console.log('Phone provider ', phoneProvider);
phoneProvider.verifyPhoneNumber(this.state.mobileNumber, this.recaptchaVerifier.current).then((phoneAuthSnapshot) => {
// verification sent
console.log('Verification code sent');
this.setState({
status: 'Confirmation code sent.',
verificationId: phoneAuthSnapshot.verificationId,
//showCodeInput: true // shows input field such as react-native-confirmation-code-field
});
验证一次性密码
firebase
.auth()
.signInWithCredential(credential)
.then((result) => {
// Do something with the results here
console.log(result);
this.props.navigation.navigate('Home');
}).catch((error) => {
console.log('error sending code ', error);
this.setState({
showFailureDialog: true,
failureMessage: 'OTP did not matched ' + error
})
解决方案
您可以通过在手机中安装 apk 来测试 OTP 功能。您还可以在 firebase 控制台中包含一些随机数和 otp 以检查身份验证过程。(身份验证->登录方法->电话->添加电话号码进行测试)
要设置 OTP 身份验证,您几乎只需将应用程序的 SHA-1 指纹添加到您的应用程序即可。(项目设置 -> 您的应用程序 -> SHA 证书指纹,适用于 android)
推荐阅读
- javascript - 从 Firebase 读取数据到 Javascript
- google-sheets - 多个嵌套的 IF(AND(OR) 语句
- react-native - undefined 不是函数(评估...........点击onPress时反应原生
- excel - 将不带标题的 MS Excel 电子表格导入现有的 MS Access 表
- java - 如何使用代码将未签名的数字签名块添加到现有文件
- qt - QML 文本渲染问题
- apache - 当 Apache SSI 失败时,我可以返回错误代码吗?
- c# - WPF,组框。未能将第二个按钮添加到 GroupBox
- python - 如何根据python中的选定键值对字典进行排序
- c# - 在 ASP.NET 中使用服务器标记分配控件 ID