gpt4 book ai didi

javascript - react-native-firebase 重发消息代码

转载 作者:行者123 更新时间:2023-11-29 23:21:14 27 4
gpt4 key购买 nike

我正在寻找一种在 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();

}
}

最佳答案

要重新发送,只需将第二个参数添加为 truesignInWithPhoneNumber("1234567891", true)并通过重新发送再次调用此方法。 https://github.com/invertase/react-native-firebase/issues/721#issuecomment-403295962

关于javascript - react-native-firebase 重发消息代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549246/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com