Christopher Mills
Christopher Mills

Reputation: 760

Is there a way to retrieve any user's PhotoUrl using only their Uid?

I have an app that has many chat rooms, each displaying the profile images of the member users. For quick loading, I store these URLs under each room in the real-time database. However, if any user updates their profile picture, the stored URL will obviously be useless. However, if I could get the current image URL for any user, using only their UID, this would no longer be an issue.

If this is not possible, are there any other common practices that would allow for the images of the users to remain current, without having to update every record across all instances of rooms containing them as members, each time they change their profile picture?

Upvotes: 3

Views: 4374

Answers (2)

bojeil
bojeil

Reputation: 30818

You can use the Firebase Admin SDK to lookup users by uid:

admin.auth().getUser(uid)
  .then(function(userRecord) {
    console.log(userRecord.photoURL):
  })
  .catch(function(error) {
    // Error occurred.
  });

Upvotes: 2

Gastón Saillén
Gastón Saillén

Reputation: 13129

Your best option will be to store the imageURL into your user id

To do this just change the reference where you are storing the photo

For example, getting the user ID

FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
uid = mAuth.getCurrentUser().getUid();

And then in your reference for example

mDatabase.child("users").child(uid).child("user_photo").setValue(your_download_url);

Remember that the download URL of the photo is obtained from your storage ref task.

So when you need to retrieve each users photo, just access to their reference and add a value event listener to get the data with a dataSnapshot and use Glide to show the image

Edit

This can work if you are doing google sign in Login in your app, you can get the user profile photo without saving anything to the database

You can get sign in information without saving it to firebase ( just with google sign in ) https://developers.google.com/identity/sign-in/android/people

so you can get profile photo URL and then use Glide to show it up

Upvotes: 3

Related Questions