Monday 23 November 2020

react native firebase phone authentication - very strange behavior

Ive copied the code for phone auth from RNFB pretty much exactly from their site...see code below (link here).

When I run code using the "Phone numbers for testing" functionalty provided by Firebase (i.e. use a dummy phone number)....then it does NOT create a user in FB when I run the signInWithPhoneNumber function.....and its not until I run confirmCode function that it creates the user in FB. As I understand it, thats the way its suppposed to work.
HOWEVER....when I run MyPhoneAuthButton fn using my own phone number (i.e. a "real" test)...then the SMS auth message is sent to me and the user is created immediately in FB without having to run confirmCode fn. Why is it acting differently when testing vs running in prod? Which process should I be coding for? Any help appreciated

const MyPhoneAuthButton = props => {
  const [objConfirm, setObjConfirm] = useState(null);
  const [code, setCode] = useState('');

  // Handle the button press
  const signInWithPhoneNumber = phoneNumber => {
    auth()
      .signInWithPhoneNumber(phoneNumber)
      .then(confirmResult => {
        setObjConfirm(confirmResult);
      })
      .catch(err => {
        console.log('Invalid Phone Number', err.message);
      });
  };

  const confirmCode = () => {
    objConfirm
      .confirm(code)
      .then(user => {
        console.log('User auth by phone OK', user);
      .catch(err => {
        console.log('Invalid code.', err.message);
      });
  };


  <MyButton
    onPress={() => {
      signInWithPhoneNumber(props.telNum);
    }}>
    Sign in With Phone Number
  </MyButton>

  const signInWithPhoneNumber = phoneNumber => {
    auth()
      .signInWithPhoneNumber(phoneNumber)
      .then(confirmResult => {
        console.log('User successfully authorized via telNum');
        setObjConfirm(confirmResult);
      })
      .catch(err => {
        Alert.alert('Invalid Phone Number', err.message);
        console.log('Invalid Phone Number', err.message);
      });
  };


from react native firebase phone authentication - very strange behavior

No comments:

Post a Comment