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
2 Answers
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.
7 Comments
ds was wrong. I edited with: dataSnapshot and String class is used for casting. Does it work now?code eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { nombre.setText(String.valueOf(dataSnapshot.child("datos").getValue())); } @Override public void onCancelled(DatabaseError databaseError) { } };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
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ; authenticatedId = currentFirebaseUser.getUid()