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:
- 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
- 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)
}