7

I build react native with firebase and I use plugin react-native-firebase. I implement firebase auth phone and when I get the sms code to my phone so there is 2 errors I get:

  1. whenI type right sms code I get the error

The sms verification code used to create the phone auth credential is invalid. Please resend the verification code sms and be

  1. when I reload the app then want to type the code again I get the error

The sms code has expired. Please re-send the verification code to try again

how can I solve these issues? in addition, there is build in to send resend sms?

that's my code

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)

}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Manspof
  • 598
  • 26
  • 81
  • 173
  • Check the following question. I think it is the same problem. https://stackoverflow.com/questions/57054891/the-sms-code-has-expired-please-re-send-the-verification-code-to-try-again – Bilal Abdeen Apr 14 '22 at 06:25

0 Answers0