How to check if a user already exists in firebase during phone auth

10,402

Solution 1

Right now, the only way to do that is via the Firebase Admin SDK. There is an API to lookup a user by phone number.

admin.auth().getUserByPhoneNumber(phoneNumber)
  .then(function(userRecord) {
    // User found.
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });

Solution 2

You can check whether user already exist in Firebase by compare it metadata. see code example:

PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, smsCode);
            FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(PhoneLoginEnterCodeActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task){
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = task.getResult().getUser();
                        long creationTimestamp = user.getMetadata().getCreationTimestamp();
                        long lastSignInTimestamp = user.getMetadata().getLastSignInTimestamp();
                        if (creationTimestamp == lastSignInTimestamp) {
                            //do create new user
                        } else {
                           //user is exists, just do login
                        }
                    } else {
                        // Sign in failed, display a message and update the UI
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                        }
                    }
                }
            });
Share:
10,402

Related videos on Youtube

Chris William
Author by

Chris William

Updated on May 22, 2022

Comments

  • Chris William
    Chris William about 2 years

    I'm trying to create an app that only uses phone authorization from firebase. Since the login/signup is done through same process, which is verifying the sent code. How can i check if a user already exists in firebase? I need this to show them appropriate User Interface.

  • Chris William
    Chris William about 6 years
    Thanks for the answer, this looks like legit method. I was trying to figure out things on my own while waiting for a reply and decided to check if user's data already exists on firestore cloud and it worked well.
  • xaif
    xaif over 5 years
    @bojeil I'm confused where to put this serviceAccountKey.json file and then set it's path ,i'm using android studio
  • bojeil
    bojeil over 5 years
    Do not use Admin SDK API in your android app. This should only run from a secure server. Service accounts should never be exposed to the client, otherwise, your users and their data would be compromised.
  • Karen
    Karen about 4 years
    @bojeil I have the same question. If we don't use the admin SDK in our app, how will we be able to check if a phone account already exists? I'm confused where we run this code if not in the app.
  • Sam Spencer
    Sam Spencer about 3 years
    @Karen you can run it as a hosted function and make an API call to it from your app. That way you only expose the singular function to your app and not the whole admin sdk