Reputation: 357
I'm looking a way to resend sms code in phone authentication in react-native-firebase.
phone verify number function
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:
if (phoneAuthSnapshot.error) {
this.showMessageErrorByCode(phoneAuthSnapshot.error.code)
}
rej(phoneAuthSnapshot)
break
}
})
})
}
confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
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.code)
}
}
what I tried to do for resend sms is to call again verifyPhoneNumber when user not get his sms code, but it's not works and I don't get sms again, if user not get in first time or if he already got sms and the sms code is expired.
resendeCode = async()=>{
const { AppStore, UserStore } = this.props
try {
AppStore.setLoading();
UserStore.setErrorCodeAuthentication('')
let { verificationId } = await FirebaseService.confirmPhone(phoneNumber); // call confirmPhone function above again
this.props.UserStore.setVerificationId(verificationId)
AppStore.setLoading();
}catch(e){
AppStore.setLoading();
}
}
Upvotes: 3
Views: 4657
Reputation: 535
To resend just add second parameter as true in signInWithPhoneNumber("1234567891", true) and call this method again with resend. https://github.com/invertase/react-native-firebase/issues/721#issuecomment-403295962
Upvotes: 1