【问题标题】:react native firebase phone authentication - very strange behavior反应原生 Firebase 电话身份验证 - 非常奇怪的行为
【发布时间】:2021-02-27 05:02:52
【问题描述】:

我已经从 RNFB 复制了电话身份验证的代码,几乎完全是从他们的网站...请参阅下面的代码(链接 here)。

当我使用“用于测试的电话号码”功能运行代码时由 Firebase 提供(即使用虚拟电话号码)......然后当我运行 signInWithPhoneNumber 函数时它不会在 FB 中创建用户......直到我运行它创建的 confirmCode 函数FB中的用户。据我了解,这就是它应该工作的方式。
但是....当我使用自己的电话号码运行MyPhoneAuthButton fn 时(即“真实”测试)...然后将 SMS 身份验证消息发送给我并且 用户立即在 FB 中创建而无需运行confirmCode fn。 为什么在测试和在产品中运行时表现不同?我应该为哪个流程编码? 任何帮助表示赞赏

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

【问题讨论】:

  • 你能链接你获取代码的网址吗?我想仔细检查一下
  • 完成 - 添加到原始帖子。我唯一改变的是使用.then() 而不是async await.....因为使用async await 它不允许我使用catch 错误
  • 你渲染了确认按钮吗?你能展示你的渲染功能吗
  • 添加到原帖
  • 嗨@jamesmurphy,你不会在任何地方调用confirmCode函数。

标签: javascript react-native firebase-authentication react-native-android react-native-firebase


【解决方案1】:

应该是这样的。您应该首先通过确认阶段,然后您将获得代码确认按钮。我已根据您从 firebase 链接的 url 对其进行了重新组织

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

  if(!objConfirm) {
    return (
      <MyButton
        onPress={() => {
          signInWithPhoneNumber(props.telNum);
        }}>
        Sign in With Phone Number
      </MyButton>  
    )
  }
  
  return (
    <>
      <TextInput value={code} onChangeText={text => setCode(text)} />
      <Button title="Confirm Code" onPress={() => confirmCode()} />
    </>
  );

【讨论】:

  • 这个答案对我来说毫无意义....请说明这是一个解决方案吗?看来你所做的只是复制了signInWithPhoneNumber 函数并删除了控制台日志和警报。也许我错过了什么??
  • 是的,你错过了。应该出现两种状态的按钮。第一个是它控制是否 objConfirm,如果未确认,则呈现用于签名的按钮,否则,如果已确认,它将呈现 textinput 和启用输入代码并提交的按钮。我刚刚比较了您编写的代码和您链接并回答的代码与编码缺失部分的代码。
  • 我确实编写了根据类型呈现不同按钮的代码.....我只是没有包含在我的问题中,因为我不认为它是相关/重要的。我希望回答的问题是为什么当我使用实数而不是虚拟数时行为会有所不同
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2010-12-17
  • 2021-03-10
  • 2021-06-10
相关资源
最近更新 更多