簡體   English   中英

反應本機 Firebase 電話身份驗證 - 非常奇怪的行為

[英]react native firebase phone authentication - very strange behavior

我已經從 RNFB 幾乎完全從他們的網站復制了電話身份驗證的代碼......請參閱下面的代碼(鏈接在這里)。

當我使用 Firebase 提供的“測試電話號碼”功能運行代碼時(即使用虛擬電話號碼)......然后當我運行signInWithPhoneNumber功能時它不會在 FB 中創建用戶......及其直到我運行confirmCode函數,它才會在FB中創建用戶。 據我了解,這就是它應該工作的方式。
但是......當我使用我自己的電話號碼運行MyPhoneAuthButton fn 時(即“真實”測試)......然后將 SMS 身份驗證消息發送給我,並且用戶會立即在 FB 中創建,而無需運行confirmCode fn 為什么在測試和在 prod 中運行時表現不同? 我應該為哪個過程編碼? 任何幫助表示贊賞

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

應該是這樣。 您應該首先通過確認階段,然后您將擁有代碼確認按鈕。 我已根據您從 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()} /> </> );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM