我使用firebase构建本机反应,并使用插件react-native-firebase。 我实现了firebase auth手机,当我将短信代码发送到我的手机时,我得到了2个错误:
用于创建手机身份验证凭据的短信验证码无效。请重新发送验证码短信并
短信代码已过期。请重新发送验证码以重试
我该如何解决这些问题? 另外,还有内置发送重发短信吗?
这是我的代码
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)
}