【问题标题】:Firebase phone auth Error: Invalid token. at nativeToJSErrorFirebase 电话身份验证错误:令牌无效。在 nativeToJSError
【发布时间】:2018-10-27 16:02:11
【问题描述】:

我正在使用firebase 电话身份验证与此docs 反应本机应用程序,我在一些不同的设备上对其进行了测试。有时电话auth 可以工作,有时它会抛出此错误

firebase 电话身份验证错误:令牌无效。在 nativeToJSError

我已经阅读了 firebase 的文档并试图理解这个错误。 这是我的代码 sn-p:

confirmPhone = async(phoneNumber) => {
  const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');
  return new Promise((res, rej) => {
    firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
      .on('state_changed', async(phoneAuthSnapshot) => {
        switch (phoneAuthSnapshot.state) {
          case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
            UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
            await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
            res(phoneAuthSnapshot)

            break

          case firebase.auth.PhoneAuthState.CODE_SENT:
            UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
            res(phoneAuthSnapshot)
            break

          case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT:
            UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
            UserStore.setErrorCodeAuthentication('SMS code has expired')
            res(phoneAuthSnapshot)


          case firebase.auth.PhoneAuthState.ERROR:
            console.log(phoneAuthSnapshot.error.code)
            if (phoneAuthSnapshot.error) {
              this.showMessageErrorByCode(phoneAuthSnapshot.error.code)

            }

            rej(phoneAuthSnapshot)
            break

        }
      })
  })
}

confirmCode = async(verificationId, code, phoneAuthSnapshot) => {
  console.log(verificationId, code);
  try {
    const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code)
    UserStore.setCodeInput(code)
    UserStore.setUserCredentials(credential)
    AppStore.setAlreadyVerifiedAuto(true)
    await this.authenticate(credential)
    return credential
  } catch (e) {
    console.log(e)
    this.showMessageErrorByCode(e.error.code)


    //    throw new Error(e)
  }
}

showMessageErrorByCode(errorcode) {
  switch (errorcode) {
    case 'auth/invalid-phone-number':
      UserStore.setErrorCodeAuthentication('Please enter valid phone number');
      break;
    case 'auth/unknown':
      {
        UserStore.setErrorCodeAuthentication('User blocked by firebase');
        break;
      }

    case 'auth/session-expired':
      {
        UserStore.setErrorCodeAuthentication('SMS code has expired');
        break;
      }
    case 'auth/invalid-verification-code':
      {
        UserStore.setErrorCodeAuthentication('Code verification not correct');
        break;
      }
    default:
      UserStore.setErrorCodeAuthentication('other error');

  }
}

authenticate = async(credential) => {
  await firebase.auth().signInAndRetrieveDataWithCredential(credential)

}

//...

【问题讨论】:

标签: javascript firebase react-native firebase-authentication


【解决方案1】:

检查以下内容:

  • 正确的捆绑包 ID
  • 正确的 Google-Info.plist
  • 正确的 aps-environment 值
  • 上传 APNS 身份验证密钥 (p8) 而不是证书

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我认为如果尝试访问 firebase 的应用的 bundle id 与在 firebase 控制台中注册的 bundle id 不匹配,就会发生这种情况。

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 2019-04-05
      • 2019-03-05
      • 1970-01-01
      • 2018-01-19
      • 2018-02-21
      • 1970-01-01
      • 2017-12-19
      相关资源
      最近更新 更多