使用Firebase从本机发送OTP时遇到问题

时间:2020-10-08 14:39:45

标签: javascript firebase react-native firebase-authentication

我正在尝试使用firebase从react-native发送OTP,但是它不起作用。我收到错误消息“找不到主机名”。我想了解此发送OTP在使用Firebase的移动应用程序中如何工作。是否可以仅通过安装.apk而不是将.apk上传到Play商店来在移动/本地测试OTP功能?

我执行了以下配置步骤:

  1. 在firebase控制台中创建的项目。
  2. 选择手机作为“身份验证”下的登录方法。
  3. 添加了移动应用并下载了google-services.json,并在我的代码的firebase属性中使用了相同的配置。

发送OTP

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
    });

验证OTP

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
    })

1 个答案:

答案 0 :(得分:0)

您可以通过在手机中安装apk来测试OTP功能。您还可以在Firebase控制台中包含一些随机数和otp来检查身份验证过程。 (身份验证->登录方法->电话->添加电话号码进行测试)

要设置OTP身份验证,几乎要做的就是将应用程序的SHA-1指纹添加到应用程序中。 (项目设置->您的应用-> SHA证书指纹,适用于Android)