【发布时间】:2018-10-19 22:54:13
【问题描述】:
我用这个 react-native-firebase 构建了带有 firebase 的 react native 应用程序
我实现了 firebase auth 电话身份验证。 在使用verifyPhoneNumber 之前,我想通过身份验证表中的电话号码检查用户是否已经存在。
因为我有一个问题,如果用户已经存在于这个表中并且我使用 verifyPhoneNumber 函数和现有的电话号码,它已经向他发送了短信。我要做的是,如果用户已经存在于这个表中,那么不要发送他和短信来获取这个用户!
confirmPhone = async (phoneNumber) => {
return new Promise((res, rej) => {
firebase.auth().verifyPhoneNumber(phoneNumber)
.on('state_changed', async (phoneAuthSnapshot) => {
console.log('phoneAUTH',phoneAuthSnapshot)
switch (phoneAuthSnapshot.state) {
case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
console.log('PhoneAuthState.AUTO_VERIFIED',phoneAuthSnapshot)
await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
res(phoneAuthSnapshot)
break
case firebase.auth.PhoneAuthState.CODE_SENT:
console.log('code send',phoneAuthSnapshot)
// 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'
console.log('AUTO_VERIFY_TIMEOUT',phoneAuthSnapshot)
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
res(phoneAuthSnapshot)
case firebase.auth.PhoneAuthState.ERROR:
console.log('APhoneAuthState.ERROR',phoneAuthSnapshot)
UserStore.setErrorConfirmationCode(phoneAuthSnapshot.error)
rej(phoneAuthSnapshot)
break
}
})
})
}
confirmCode = async (verificationId, code, phoneAuthSnapshot) => {
try{
const credential = await firebase.auth.PhoneAuthProvider.credential(UserStore.verificationId, code)
UserStore.setCodeInput(code)
UserStore.setUserCredentials(credential)
AppStore.setAlreadyRegister(true)
await this.authenticate(credential)
return credential
} catch(e){
throw new Error(e)
}
}
authenticate = async (credential) => {
await firebase.auth().signInAndRetrieveDataWithCredential(credential)
}
【问题讨论】:
标签: firebase firebase-authentication