1

I need to get the email address from uid of one user (not the current user). My database has one child called "users" and this one has children which keys are the uid of the all the users. I need to get the email address of the other users by their uid.

Look the database structure

String uidReceiver = "B53Bk4dtHjZ6a3ZjoO2cEowEf4w2";
DatabaseReference usersRef;
usersRef = FirebaseDatabase.getInstance().getReference().child("users");

// To get the email address of the current user
FirebaseUser userSender = FirebaseAuth.getInstance().getCurrentUser();
String emailSender = userSender.getEmail();    

usersRef.child(uidReceiver).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) 
                    {
                        // How can I get the email address of uidReceiver
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            });

Thanks!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
esterrrsinH
  • 171
  • 2
  • 11
  • Sounds like a fairly basic value listener as described here: https://firebase.google.com/docs/database/android/read-and-write#listen_for_value_events Did you try anything yet? – Frank van Puffelen Dec 17 '18 at 19:37
  • Yes, I know I need to use a listener, the problem is what to write to get the email. I have updated the post with the code – esterrrsinH Dec 17 '18 at 19:42
  • Your database that you've shown doesn't have the users emails... Does each user object in that database have children containing the users email? Could you expand one of the users to show how your data is set-up – Quinn Dec 17 '18 at 20:01
  • No, no child contains the emails. The emails are just in FirebaseAuth. I am looking for the way to get the email without using a specific child, but if there isn't another way I should save the email in the "users" database... – esterrrsinH Dec 17 '18 at 20:05
  • so what you want to say is you want to refer from the uid, and get the value email from the firebase authentication? – Ticherhaz FreePalestine Dec 17 '18 at 20:07
  • yes, i wanted that... but it seems impossible – esterrrsinH Dec 17 '18 at 20:16
  • 1
    Looking up another user's email based on their UID is indeed not possible with the Android SDK for Firebase Authentication. It *is* possible with the Firebase Admin SDK though, but that one is meant to be used in trusted environments (your development system, a server you control access to, or Cloud Functions for Firebase) and not in Android apps. – Frank van Puffelen Dec 17 '18 at 21:07

1 Answers1

2

If you mean that you want to get the email of the user from FirebaseAuth using his uid, then it's not possible from android client. Only firebase admin who can get the email of the user from the FirebaseAuth, and android sdk has NO admin permissions. You need to add the email to the Firebase Database in order to get the email of the needed user.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43