Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UserName from FirebaseUser using authentication Uid in android?

I registered a user by using createUserWithEmailAndPassword() and login using signInWithEmailAndPassword() methods. Now when I login a user I need to get the username, mobile, that are stored in the user node. I could get the UId for each user, by using this how it possible to get the mentioned information in android?

like image 227
Proversion Avatar asked Apr 11 '18 06:04

Proversion


1 Answers

all you have to do is use UserProfileChangeRequest

 mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    // Sign in success
                    FirebaseUser user = mAuth.getCurrentUser();

                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName(mName).build();

                    user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "User profile updated.");
             }
         }
     });


                }
    });

Then, to retrieve it, use this wherever required,

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address etc
    String name = user.getDisplayName();
    String email = user.getEmail();
}

or you can create your own custom node.!

 String currentuser = FirebaseAuth.getInstance().getUid();

//save user node after successful signup.
      mFirebaseInstance.getReference("user").child(scurrentuser ).setValue(parameters);



    databaseReference = FirebaseDatabase.getInstance().getReference().child("user").child(currentuser );
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


      for(DataSnapshot userSnapShot: dataSnapshot.getChildren()){
            User user = userSnapShot.getValue(User.class);
        }
        }
like image 137
Atif AbbAsi Avatar answered Nov 15 '22 03:11

Atif AbbAsi