简体   繁体   English

使用 firebase 从 React Native 发送 OTP 时遇到问题

[英]Facing issue while sending OTP from react native using firebase

I am trying to send an OTP from react-native using firebase, but it's not working.我正在尝试使用 firebase 从 react-native 发送 OTP,但它不起作用。 I am getting an error "Hostname Not Found".我收到错误消息“找不到主机名”。 I wanted to understand how does this sending OTP works in mobile apps using firebase. Can I test the OTP feature in mobile/local by just installing the.apk rather than uploading the.apk to the play store?我想了解此发送 OTP 在使用 firebase 的移动应用程序中如何工作。我可以通过仅安装 the.apk 而不是将 the.apk 上传到 Play 商店来测试移动/本地的 OTP 功能吗?

I did the below configuration steps:我做了以下配置步骤:

  1. Created project in firebase console.在 firebase 控制台创建项目。
  2. Selected phone as a sign-in method under Authentication.在身份验证下选择电话作为登录方法。
  3. Added the mobile app and downloaded the google-services.json and used the same configuration in the firebase properties in my code.添加了移动应用程序并下载了 google-services.json 并在我的代码中的 firebase 属性中使用了相同的配置。

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

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

You CAN test the OTP feature by installing the apk in your phone.您可以通过在手机中安装 apk 来测试 OTP 功能。 You can also include some random number and otp in the firebase console to check the authentication process.您还可以在 firebase 控制台中包含一些随机数和 otp 以检查身份验证过程。 (Authentication -> Sign in methods -> phone -> Add phone numbers for testing) (身份验证 -> 登录方法 -> 电话 -> 添加电话号码进行测试)

To setup OTP authentication, pretty much all you have to do is add your app's SHA-1 fingerprint to your app.要设置 OTP 身份验证,几乎所有您需要做的就是将您应用的 SHA-1 指纹添加到您的应用中。 (Project settings -> Your apps -> SHA certificate fingerprints, for android) (项目设置 -> 您的应用程序 -> SHA 证书指纹,适用于 Android)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM