【问题标题】:firebase phone auth check if user already exist before send smsfirebase phone auth 在发送短信之前检查用户是否已经存在
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我现在刚刚使用 firebase 函数完成了它,它返回给我用户的对象。但是如何在客户端使我得到它时它将成为我客户端的身份验证用户?我的意思是我发送了 firebase 函数请求,然后获取用户对象。那该怎么办。 firebase.auth().onAuthStateChanged((user) 现在该用户如何进行身份验证?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2019-01-24
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多