0

I build react native app with firebase with this react-native-firebase

and I implement firebase auth phone authentication. I want to check if user already exist by phone number in Authentication table before use verifyPhoneNumber. enter image description here

Because I have problem that if user already exist in this table and I'm use verifyPhoneNumber function with exist phone number it already send him sms. what I'm looking to do is, if user already exist in this table so not to send him and sms and get this user!

 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 Answer 1

0

You can use getUserByPhoneNumber() in admin sdk. But for Front end framework i think we dont have like this, Suggest you to create the records in firestore and check for the same.

1
  • I just did it now with firebase function and it return me object with the user. but how to make in client side that when I get it it will be as authenticate user in my client side? I mean I sent firebase function request and then get user object. what to do then. how firebase.auth().onAuthStateChanged((user) will now that user is authenticate?
    – Manspof
    May 10, 2018 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.