How to get userID by user Email Firebase android?

24,455

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:

Share:
24,455
SumOne
Author by

SumOne

Updated on December 17, 2020

Comments

  • SumOne
    SumOne over 3 years

    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

  • SumOne
    SumOne over 7 years
    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)?
  • Frank van Puffelen
    Frank van Puffelen over 7 years
    The client SDKs for Firebase Authentication only provide access to the profile of the currently authenticated user. I clarified this in my answer.
  • Mehul Gajjar
    Mehul Gajjar over 6 years
    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
  • Boy
    Boy about 5 years
    @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?
  • jakobinn
    jakobinn over 4 years
    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.