firebase phone auth在发送短信之前检查用户是否已经存在

时间:2018-05-09 18:24:28

标签: firebase firebase-authentication

我使用此react-native-firebase

构建使用firebase构建本机应用程序

我实施了firebase auth phone身份验证。 我想在使用verifyPhoneNumber之前通过身份验证表中的电话号码检查用户是否已经存在。 enter image description here

因为我有问题,如果用户已经存在于此表中,并且我使用带有现有电话号码的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)

}

1 个答案:

答案 0 :(得分:0)

您可以在admin sdk中使用getUserByPhoneNumber()。但对于前端框架,我认为我们不喜欢这样,建议你在firestore中创建记录并检查相同的内容。