【问题标题】:Firebase phone authentication without waiting for verification codeFirebase 手机认证无需等待验证码
【发布时间】:2021-11-16 17:11:59
【问题描述】:

我正在开发一个带有电话身份验证的本地反应应用程序以进行用户注册。我为此目的使用firebase。问题是当用户输入电话号码并点击“发送验证码”按钮时,firebase(Google) 成功通过短信向用户发送了验证码,但在用户输入确认码并点击“确认”按钮之前,登录过程自动开始!!!为什么会这样?我该如何解决这个问题?

注意:

  • 如果使用测试电话号码(由 firebase 提供),则不会发生这种情况。
  • 应用无权读取用户短信内容。
  • 我在 firebase 应用设置中添加了 SHA-1 和 SHA-256 指纹用于调试和发布

我的应用中的身份验证逻辑:

async function onSignUp() {
  setLoading_signUp(true);

  if (phonenumber.length === 10 && name !== '') {
    try {
      const confirmation = await auth().signInWithPhoneNumber(
        '+98' + phonenumber
      );
      setConfirm(confirmation);
      // console.log('confirm ===>   ',confirm);
    } catch (error) {
      setLoading_signUp(false);
      alert(error.message);
    }
  } else if (phonenumber.length !== 10 && name === '') {
    setLoading_signUp(false);
    alert('Invalid Phone number !\nName field should not be empty!');
  } else if (phonenumber.length === 0) {
    setLoading_signUp(false);
    alert('Enter Phone number !');
  } else if (0 < phonenumber.length < 10) {
    setLoading_signUp(false);
    alert('Invalid Phone number !');
  } else {
    setLoading_signUp(false);
    alert('Name field should not be empty!');
  }
}

async function confirmCode() {
  setLoading(true);

  try {
    await confirm.confirm(code);
    setConfirm(null);
    await AsyncStorage.setItem('@DidSignUp', 'true');
    await AsyncStorage.setItem(
      '@credentials',
      `{"name":"${name}","email":"${email}","phonenumber":"${
        '+98 ' + phonenumber
      }"}`
    );

    firestore().collection('users').doc(auth().currentUser.uid).set({
      name,
      email,
      phonenumber,
    });
  } catch (error) {
    setLoading(false);

    if (error.code !== 'auth/unknown') {
      alert('Invalid code !');
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    在 Android 上,Firebase 身份验证 SDK 实际上与操作系统一起工作,以自动检测包含 OTP 的 SMS。这称为自动检索,documented 为:

    自动检索:在某些设备上,Google Play 服务可以自动检测传入的验证短信并执行验证,无需用户操作。 (某些运营商可能不提供此功能。)它使用SMS Retriever API,它在 SMS 消息的末尾包含一个 11 个字符的哈希。

    所以这是一个特性,而不是一个错误。 :) 似乎没有办法禁用该功能,据我所知:How to disable Firebase Phone Auth Android auto-sign-in (onVerificationCompleted callback)

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2021-04-11
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2017-02-06
      相关资源
      最近更新 更多