How to get userID by user Email Firebase android?

SumOne picture SumOne · Jan 15, 2017 · Viewed 22.1k times · Source

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

Answer

Frank van Puffelen picture Frank van Puffelen · Jan 15, 2017

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: