0

I have a question with Firebase, I'm new to this. I would like to return the user's name when I login, but that name is in Database and I would like to return only the name, of course, coinciding with the email found in the authentication, which is also in the Database. The idea I have is to compare the email from the database with the Auth and if it is true, give me the name. But I would not know how to develop it. Solutions? Thank you

KENdi
  • 7,576
  • 2
  • 16
  • 31

2 Answers2

0

I don't think you need to go into that much trouble. Follow this and it might help you. You don't need to do a email matching. Firebase provides unique user ID for each user. Once a user register into your system firebase provides that ID. You just have to save it in database as the primary key for that user. Then you can get data from your database using user Id. User Id can be fetched in any place using firebase.auth().currentUser.uid()

Pasindu
  • 81
  • 8
  • How can I do it, how to get the authentication id to place it in the database? – Leonardo Cárdenas Salas Mar 16 '18 at 21:34
  • You can use following code get the current logged used it. After you registered a user using any registration method in firebase you can use this code segment. `FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ; authenticatedId = currentFirebaseUser.getUid()` – Pasindu Mar 17 '18 at 01:19
  • Thanks, but placing the uid as child, as I recover a specific data and I wanted to place it only in a TextView? – Leonardo Cárdenas Salas Mar 17 '18 at 06:33
0

In order to achieve this, you need to use a model class that looks like this:

public class UserModel implements Serializable {
    private String userEmail;
    private String userName;

    public UserModel() {}

    public UserModel(String userEmail, String userName) {
        this.userEmail = userEmail;
        this.userName = userName;
    }

    public String getUserEmail() {return userEmail;}
    public String getUserName() {return userName;}
}

To actually add a user, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
UserModel userModel = new UserModel("john@email,com", "John");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
usersRef.child(uid).setValue(userModel);

Your database structure will look like this:

Firebase-root
   |
   --- users
         |
         --- uid
              |
              --- userEmail: "john@email,com"
              |
              --- userName: "John"

Furthermore, to get back the userName, please use the following code:

DatabaseReference uidRef = usersRef.child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()) {
            String userName = dataSnapshot.child("userName").getValue(String.class);
            nombre.setText(userName);
            Log.d("TAG", userName);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
uidRef.addListenerForSingleValueEvent(eventListener);

If you are interested, I have also exaplained in one of my tutorials step by step, the entire authentication process using Google and Firebase.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193