19

I have Firebase Authentication in my Android app. My question is how do I link between authenticated users (that appear in Firebase::Auth tab in the Firebase console) and the database (Firebase::Database tab).

I need to link data in the database with the relevant user in Auth by the User UID that appears in Auth, and eventually send queries about a specific UID to get the relevant info in Database.

Is there something automatic / built-in that does that or do I have to create a new entry for a user in Database each time I authenticate a user and then create an ID field for it with the UID value that appears in Auth?

In the User Management docs it explains only about adding users to the Auth tab and resetting their passwords.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alaa M.
  • 4,961
  • 10
  • 54
  • 95
  • You'll need to write the code to store the user profile in the database. See http://stackoverflow.com/q/39444060, http://stackoverflow.com/q/39439459, http://stackoverflow.com/questions/38276447, – Frank van Puffelen Oct 12 '16 at 14:24
  • Possible duplicate of [How do I link each user to their data in Firebase?](http://stackoverflow.com/questions/30910704/how-do-i-link-each-user-to-their-data-in-firebase) – André Kool Oct 12 '16 at 20:27
  • 1
    @André Kool this looks like javascript... I'm asking about Android – Alaa M. Oct 12 '16 at 20:40
  • I know but the principle is exactly the same, instead of the web docs for reading and writing data you can take a look at [the android docs](https://firebase.google.com/docs/database/android/read-and-write#basic_write). The first example you see there should be about saving user data. – André Kool Oct 13 '16 at 05:43
  • 1
    @AndréKool I'm not struggling with writing... I was wondering if there's a built in way to map between the UID's and the database... Seems like there's no built in thing. The solution is like I said in my question (and now I posted an answer) – Alaa M. Oct 13 '16 at 06:15

1 Answers1

15

After authentication, create a child with the UID given by Firebase, and set its value to your user class:

//get firebase user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

//get reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(USERS_TABLE);

//build child
ref.child(user.getUid()).setValue(user_class);

USERS_TABLE is a direct child of root.

Then when you want to retrieve the data, get a reference to the user by its UID, listen for addListenerForSingleValueEvent() (invoked only once), and iterate over the result with reflection:

//get firebase user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

//get reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(USERS_TABLE).child(user.getUid());
//IMPORTANT: .getReference(user.getUid()) will not work although user.getUid() is unique. You need a full path!

//grab info
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        final Profile tempProfile = new Profile(); //this is my user_class Class
        final Field[] fields = tempProfile.getClass().getDeclaredFields();
        for(Field field : fields){
            Log.i(TAG, field.getName() + ": " + dataSnapshot.child(field.getName()).getValue());
        }
    }

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

edit:

Or without reflection:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    final Profile p = dataSnapshot.getValue(Profile.class);
}
Alaa M.
  • 4,961
  • 10
  • 54
  • 95