【发布时间】:2018-11-06 00:52:42
【问题描述】:
我正在寻找一种在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();
}
}
【问题讨论】:
标签: javascript firebase react-native firebase-authentication