How to get correct user info from Firebase?

Viewed 51

I'm trying to get logged user's name, but when I want to set it's name on Textview, returns the different values:

Using this method:

public void a() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null){
            for (UserInfo profile : user.getProviderData()){
                String providerId = profile.getProviderId();
                String name = profile.getDisplayName();
                String uid = profile.getUid();
                helloUser.setText(providerId);
            }
        }
    }

This is user data from Firebase:

7gPwXUU3k6XSoBMfhxEmWoA6F1h1
  email:"test8@gmail.com"
  name :"Test8"
  password:"12345678"

in these cases returns this results:

providerId returns "Password"
name returns "null"
uid returns "test8@gmail.com"

UPDATE!

I've changed code like this:

FirebaseAuth userAuth = FirebaseAuth.getInstance();
        FirebaseUser userUser = userAuth.getCurrentUser();
        if (userUser != null){
            String aa = userUser.getUid();

            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(aa);
            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                       String bb = snapshot.child("name").toString();
                       helloUser.setText(bb);
                       Log.d(TAG, "UserGetResult:success" + " "+ bb);
                   }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }

End returns this:

D/ContentValues: UserGetResult:success DataSnapshot { key = name, value = null }

What am I doing wrong?

2 Answers

Firebase Authentication is not directly linked to the Firebase databases (Realtime Database and Firestore) in the way that you seem to expect. When you have a FirebaseUser object in hand, the only data it has is what the Firebase Authentication product stores about the user for the purpose of signing in. It doesn't contain any related data that you might have also stored in a database. The output you're showing seems correct to me - you are printing the basic information that Firebase Auth knows about the currently signed in user.

If you want a user's related data, you will have to write code to query that database. It's difficult to tell what that is given what you've shared, but maybe you will have to get() a document from Firestore using the user's UID from Firebase Auth.

I will point out that it's generally a bad idea to store user passwords in your own database. That should all be securely hidden by Firebase Auth.

Instead of doing

String bb = snapshot.child("name").toString();

Try

String bb = snapshot.child("name").getValue();

Calling toString returns the reference of the DataSnapshot of the child instead.

Edit: Assuming that you store user data on a User class with getters, you can get the data in this way.

User user = snapshot.getValue(User.class);
String name = user.getName();

Sample code of a User.java to store user data

public class User {
    public String name;
    public String email;
    public String password;

    public User() {

    }

    public User(String name, String email, String password) {
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

}
Related