【发布时间】:2021-01-23 15:25:20
【问题描述】:
我正在尝试使用 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
})
【问题讨论】:
标签: javascript firebase react-native firebase-authentication