我正在寻找一种在react-native-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:
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)
}
}
我尝试重新发送短信的方法是,当用户未收到短信代码时再次调用verifyPhoneNumber,但如果用户第一次没有收到短信,或者如果他已经收到短信和短信,则无法再次收到短信,代码已过期。
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();
}
}
最佳答案
要重新发送,只需在第二个参数中添加true
signInWithPhoneNumber(“ 1234567891”,正确)
然后重新发送此方法。
https://github.com/invertase/react-native-firebase/issues/721#issuecomment-403295962