15

Given a email address, is it possbile to get userID of a person? For example,

If I have a variable email that has email of the person. Can I get their ID by doing something like

String userID = mAuth.DatabaseReference.getuID.(email);

Sorry if this a stupid question.

Edit: note, I am looking to get ID of a person who is not the current user. So I can't use FirebaseUser user = mAuth.getCurrentUser();

The structure of my database looks like this, so the ID will be stored in database already. I just need a way of getting it (In the diagram presented below I don't have a field of email but I will be adding one, incase anyone was wondering -_-).

Also, if possible, I would like to get their profile image either based on their email or once I have gotten the id, through ID.

enter image description here

AL.
  • 36,815
  • 10
  • 142
  • 281
SumOne
  • 817
  • 3
  • 13
  • 24

6 Answers6

32

If you want to look up a user by their email on a trusted server, you can use the Firebase Admin SDK. From the documentation on retrieving user data:

admin.auth().getUserByEmail(email)
  .then(function(userRecord) {
    // See the tables above for the contents of userRecord
    console.log("Successfully fetched user data:", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });

The client SDKs for Firebase Authentication only provide access to the profile of the currently authenticated user. They don't provide a way to look up a user by their email address. To allow client-side lookup, the common way to do so is to store the UID-by-email-address in the database:

"emailToUid": {
    "SumOne@domain,com": "uidOfSumOne",
    "puf@firebaseui,com": "uidOfPuf"
}

With this simple list, you can look up the UID by the encoded email address from:

ref.child("emailToUid").child("SumOne@domain,com").addSingleValueEventListener(...

See also:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So, I would need to store the uid a value, its not possible to get uid from the root element by using email? also can I get image profile of the person (not the current user)? – SumOne Jan 16 '17 at 10:58
  • The client SDKs for Firebase Authentication only provide access to the profile of the currently authenticated user. I clarified this in my answer. – Frank van Puffelen Jan 16 '17 at 17:08
  • in app side any tactics to check login provider type without user login... want to prevent reset password of user who did social logins... currently getting reset password link on all logins mail id in android – Mehul Gajjar Jan 03 '18 at 06:55
  • @FrankvanPuffelen wouldn't it be more logic to do uidToEmail as key? I think the e-mail adres could change for the user, but the UID won't? – Boy Mar 25 '19 at 05:37
  • It's correct that you have to change the dot to a comma, but note that strings such as $ are allowed in email addresses but not as a firebase key, so you would also have to change that to some sign. – jakobinn Nov 17 '19 at 14:41
0

This way you can login user with email and password and retrieve id:

FirebaseAuth.getInstance().signInWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
          @Override
          public void onSuccess(AuthResult authResult) {
              FirebaseUser user = authResult.getUser();
          }
      });
illya yurkevich
  • 226
  • 3
  • 6
0

Firebase do not provide user info on the basis of email. You can do one thing make login with your credential and after successful login use below code to get details of user.

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                            if(currentFirebaseUser !=null)

    {
        Log.d(TAG, "onComplete: currentUserUid---->" + currentFirebaseUser.getUid());
        Log.d(TAG, "onComplete: currentUserEmail---->" + currentFirebaseUser.getEmail());
        Log.d(TAG, "onComplete: currentUserDisplayName---->" + currentFirebaseUser.getDisplayName());
    } else

    {
        Log.d(TAG, "onComplete: currentUserUid is null");
    }
Rahul Jain
  • 276
  • 2
  • 11
0

I had the same problem, which I solved by creating a new attribute to the model class whose data you want to access from another account.

As given below my model class has 5 attributes now. Initially it was only the first four.

public class User {

    public String id;
    public String name;
    public String email;
    public String role;
    public String uid;

    public User() {
    }

    public User(String id, String name, String email, String role, String uid) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.role = role;
        this.uid = uid;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getRole() {
        return role;
    }

    public String getUid() {
        return uid;
    }

    public void setId(String id) {
        this.id = id;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

}

In this class I had not put the uid attribute before. So, when I am signed in, I don't have the access to uid of another account. That is why I changed the model class as given above, and now I am able to access any data just by using any other parameter to check for equality.

For example,

mRef.addValueEventListener(new ValueEventListener() {
        private String role;

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                User user = snapshot.getValue(User.class);

                role = user.getRole();
                if(role!=null){
                    if(role.equals("student")){
                        STUDENT_LIST.add(user);
                    }
                }
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {}
    });

Here you can see I am taking all the users who are students, whose values can be accessed using the uid that was added to the above model class, from another signed in account.

Kiran
  • 402
  • 1
  • 4
  • 11
0

The easy way to solve this is to create another model(table), that holds the users email address and id. This way you can retrieve the id having the email. There are solutions that use java-script, but they have complex running time.

Semon
  • 1
0

In my case I have a list of users in the realtime database and I need to get a uid with special email:

function uidByEmail(email: String) {
     firebaseInstance.getReference("Users/").orderByChild("email").equalTo(email)
        .addListenerForSingleValueEvent(...)
...
}
Kontick
  • 85
  • 6