Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get userID by user Email Firebase android?

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

like image 418
SumOne Avatar asked Jan 15 '17 20:01

SumOne


People also ask

How can I get username of UID Firebase?

You can't get the name, or any other details of a user that is not currently signed in using FirebaseAuth . Instead, you must create a node in your database where you store the name, and any other necessary details by querying the database.

Is Firebase UID a UUID?

UIDs in firebase are UUIDs as well. UIDs are nowadays indeed UUIDs.


1 Answers

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:

  • Firebase Authentication Service - lookup uid from email without login
  • Add Extra Details on Firebase User Table
  • How to get authenticated users from firebase database?
like image 187
Frank van Puffelen Avatar answered Sep 20 '22 08:09

Frank van Puffelen