简体   繁体   中英

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. 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?

I did the below configuration steps:

  1. Created project in firebase console.
  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.

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. You can also include some random number and otp in the firebase console to check the authentication process. (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. (Project settings -> Your apps -> SHA certificate fingerprints, for android)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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