invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.69k stars 2.21k forks source link

ISSUE: sending duplicate SMS when using PhoneAuth #932

Closed Gshock1 closed 6 years ago

Gshock1 commented 6 years ago

Hi,

I'm using firebase to authenticate my users, but when I ask firebase to authenticate via SMS, it sends double SMS to the user. Sometimes the SMSs are identical, but sometimes it contains different codes.

Here is my Code:

` sendCode() { if (this.state.sendMessage) { setTimeout(async () => {

    try {

      firebase.auth()
        .verifyPhoneNumber("+972" + this.state.phone)
        .on('state_changed', (phoneAuthSnapshot) => {
          switch (phoneAuthSnapshot.state) {
            // ------------------------
            //  IOS AND ANDROID EVENTS
            // ------------------------
            case firebase.auth.PhoneAuthState.CODE_SENT: // or 'sent'

              firebase.auth().signInWithPhoneNumber("+972" + this.state.phone)
              .then(confirmResult => this.setState({ 
                confirmResult,
                showCodeVerify: true, 
                message: 'Code has been sent!' }))
              .catch(error => this.setState({ message: `Sign In With Phone Number Error: ${error.message}` }));

              this.setState({ verificationCode: phoneAuthSnapshot.code })
              break;

              case firebase.auth.PhoneAuthState.ERROR: // or 'error'
              console.log('verification error');
              //alert(phoneAuthSnapshot.error);
              break
          }
        }, (error) => {
          // optionalErrorCb would be same logic as the ERROR case above,  if you've already handed
          // the ERROR case in the above observer then there's no need to handle it here
          console.log(error);
          // verificationId is attached to error if required
          console.log(error.verificationId);
        }, (phoneAuthSnapshot) => {
          //   and there'd be no need to ask for user input of the code - proceed to credential creating logic
          // - if ANDROID auto verify timed out then phoneAuthSnapshot.code would be null, just like ios, you'd
          //   continue with user input logic.
          console.log(phoneAuthSnapshot);
        });
      // optionally also supports .then & .catch instead of optionalErrorCb &
      // optionalCompleteCb (with the same resulting args)

    }
    catch (err) {
      setTimeout(() => {
        //Alert.alert('Oops!', err.message);
      }, 100);
    }
  })
}

} ' ' _verifyCode = () => {

setTimeout(async () => {

  try {

    const { confirmResult } = this.state;
    const code = this.state.code

    var myState = this;

    if (this.state.verifiedCode)
      return true;

    if (confirmResult && code.length) {
      confirmResult.confirm(code)
        .then((user) => {
          //this.setState({ message: 'Code Confirmed!' });
          firebase.auth().currentUser.getIdToken(true).then(function (idToken) {
            console.log("SUCCESS");
            console.log("Cellphone TOKEN:", idToken);
            console.log("Facebook TOKEN:", myState.state.UserToken)
            myState.setState({verifiedCode : true, UserToken : idToken})

/ try { firebase.auth.currentUser.linkWithCredential(firebase.auth.FacebookAuthProvider.credential(myState.state.UserToken)); } catch (error) { console.log("error trying to link 2 accounts") return true; } /

            console.log ("Success Validating code")
            return true;
          }).catch(function (error) {
            console.log("ERROROOROROROROR");
          });

        })
        .catch(error => {
          console.log("Error verify code", error)
          var err = { message: "The code is incorrect!" }
          throw err;
        });
    }

    else{
      console.log("Error validating Code")
      return false;
    }

  } catch (err) {
    this.setState({ spinner: false });
    setTimeout(() => {
      //Alert.alert('Oops!', err.message);
    }, 100)
    return false;
  }

}, 100);

} '

Environment: OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: Not Found npm: 5.5.1 Watchman: Not Found Xcode: Xcode 9.2 Build version 9C40b Android Studio: 3.1 AI-173.4670197

Packages: (wanted => installed) react: 16.0.0-beta.5 => 16.0.0-beta.5 react-native: 0.49.1 => 0.49.1 "react-native-firebase": "^3.1.0",

chrisbianca commented 6 years ago

@Gshock1 I'm afraid that this is going to be an error in your implementation and you'll be calling the RNFirebase API twice somehow. Closing as it's not a react-native-firebase issue.