Firebase Auth SMS 验证码用于创建电话身份验证

Adi*_*ari 5 firebase firebase-authentication

我使用firebase构建react native,并使用插件react-native-firebase。我实现了 firebase auth 电话,当我将短信代码发送到我的手机时,我得到了 2 个错误:

  1. 当我输入正确的短信代码时出现错误

用于创建手机认证凭证的短信验证码无效。请重新发送验证码短信

  1. 当我重新加载应用程序然后想再次输入代码时,我收到错误

短信代码已过期。请重新发送验证码重试

我该如何解决这些问题?另外,有内置发送重发短信吗?

那是我的代码

confirmPhone = async (phoneNumber) => {
    return new Promise((res, rej) => {
        firebase.auth().verifyPhoneNumber(phoneNumber)
            .on('state_changed', async (phoneAuthSnapshot) => {
                switch (phoneAuthSnapshot.state) {
                case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
                    await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
                    res(phoneAuthSnapshot)

                    break

                case firebase.auth.PhoneAuthState.CODE_SENT:
                    // await userSettings.set(AUTH_KEYS.VERIFICATION_ID, phoneAuthSnapshot.verificationId)
                    UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                    res(phoneAuthSnapshot)
                    break

                case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: // or 'timeout'
                    UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
                    res(phoneAuthSnapshot)


                case firebase.auth.PhoneAuthState.ERROR:
                    UserStore.setErrorConfirmationCode(phoneAuthSnapshot.error)
                    rej(phoneAuthSnapshot)
                    break

                }
            })
    })
}

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

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

}
Run Code Online (Sandbox Code Playgroud)