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

1
  • I compare it so that I can receive the name that has as the node the email. Because in the Auth I can not insert a name and then return that name. Commented Mar 13, 2018 at 9:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

What is ds and String.class?
Sorry, I just updated my answer. ds was wrong. I edited with: dataSnapshot and String class is used for casting. Does it work now?
Please keep me posted.
I worked like this:
code eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { nombre.setText(String.valueOf(dataSnapshot.child("datos").getValue())); } @Override public void onCancelled(DatabaseError databaseError) { } };
|
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()

3 Comments

How can I do it, how to get the authentication id to place it in the database?
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()
Thanks, but placing the uid as child, as I recover a specific data and I wanted to place it only in a TextView?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.