Home » Android » Get updated User from Firebase Authentication

Get updated User from Firebase Authentication

Posted by: admin November 1, 2017 Leave a comment

Questions:

I’m currently working with Firebase. At the moment I’m using com.google.firebase:firebase-auth:10.0.1

Creating a user, Sign-In works like a charm.

Now I want to add an option for the user where it’s possible for the user to change his/her displayName and profileImage

In Activity A I call this method, by clicking a button:

   public void updateFirebaseUser(String newDisplayName, Uri newPhotoUri) {
      final FirebaseAuth auth = FirebaseAuth.getInstance();
      final FirebaseUser user = auth.getCurrentUser();
      if (user == null || (TextUtils.isEmpty(newDisplayName) && newPhotoUri == null)) {
         return;
      }
      UserProfileChangeRequest.Builder builder = new UserProfileChangeRequest.Builder();

      if (!TextUtils.isEmpty(newDisplayName)) {
         builder.setDisplayName(newDisplayName);
      }
      if (newPhotoUri != null) {
         builder.setPhotoUri(newPhotoUri);
      }
      UserProfileChangeRequest request = builder.build();
      user.updateProfile(request)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
               @Override
               public void onComplete(@NonNull Task<Void> task) {
                  Toast.makeText(context, "profile update success = " + task.isSuccessful(),
                        Toast.LENGTH_SHORT)
                        .show();
               }
            });
   }

Everything seems to work, because I receive the Toast on my screen, which shows "profile update success = true"

Later in the app I want to display the user info, I call fillUserViews() in Activity B. But still my old information is displayed.

   private void fillUserViews(NavigationView navigationView) {
      View headerView = navigationView.getHeaderView(0);
      FirebaseAuth auth = FirebaseAuth.getInstance();
      FirebaseUser user = auth.getCurrentUser();

      ImageView avatarVIew = (ImageView) headerView.findViewById(R.id.img_avatar);
      picasso.load(user.getPhotoUrl())
            .into(avatarVIew);

      TextView userName = (TextView) headerView.findViewById(R.id.txt_name);
      userName.setText(user.getDisplayName());

      TextView userMail = (TextView) headerView.findViewById(R.id.txt_mail);
      userMail.setText(user.getEmail());
   }

Also restarting the app doesn’t help here. But if I logout and re-login again my user data is updated.

I think this has something to do with the Firebase internal caching. That’s why I added this line auth.getCurrentUser().reload(); after showing the the toast.

Is there a possibility to force the update-request to Firebase? I found nothing in the documentation.

Thanks in advance!

Answers:

wuhuuuu! I found a solution:

I’m reloading the user object when I acutally need it and after the request was successful I’m updating my views!

   private void fillUserViews(NavigationView navigationView) {
      final View headerView = navigationView.getHeaderView(0);
      final FirebaseAuth auth = FirebaseAuth.getInstance();
      auth.getCurrentUser()
            .reload()
            .addOnSuccessListener(new OnSuccessListener<Void>() {

               @Override
               public void onSuccess(Void aVoid) {
                  FirebaseUser user = auth.getCurrentUser();

                  ImageView avatarVIew = (ImageView) headerView.findViewById(R.id.img_avatar);
                  picasso.load(user.getPhotoUrl())
                        .into(avatarVIew);
                  TextView userName = (TextView) headerView.findViewById(R.id.txt_name);
                  userName.setText(user.getDisplayName());
               }
            });
   }

Questions:
Answers:

I would suggest you to save the credentials in the firebaseDatabase as node for each user separately and save the username and profile Image there that will update your view as soon as the data is changed in database, if proper listeners are set.
i.e

enter image description here