Firebase phone auth错误:令牌无效。在nativeToJSError

时间:2018-05-17 11:39:26

标签: javascript firebase react-native firebase-authentication

我正在使用firebase手机身份验证功能将本地应用与docs一起使用,我在一些不同的设备上测试了它。有时电话auth有效,有时它会抛出此错误

  

firebase phone auth错误:令牌无效。在nativeToJSError

enter image description here

我已经浏览了firebase的文档,并尝试了解此错误。 这是我的代码片段 -

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)

}`

                      //...

2 个答案:

答案 0 :(得分:1)

检查以下内容:

  • 更正束ID
  • 更正Google-Info.plist
  • 正确的aps环境值
  • 上传APNS身份验证密钥(p8)而不是证书

希望对您有所帮助。

答案 1 :(得分:0)

我认为,如果尝试访问Firebase的应用程序的捆绑软件ID与在Firebase控制台中注册的捆绑软件ID不匹配,就会发生这种情况。