从Firebase身份验证获取更新的用户 [英] Get updated User from Firebase Authentication

查看:102
本文介绍了从Firebase身份验证获取更新的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Firebase。目前,我正在使用 com.google.firebase:firebase-auth:10.0.1



创建用户,登录工作就像一个魅力。



现在我想为用户添加一个选项,用户可以更改他/她的displayName和profileImage

活动AI通过点击一个按钮来调用这个方法:

  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(); $!
$ b 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();
}
});



$ b $ p
$ b一切似乎都奏效,因为我在屏幕上收到Toast, profile update success = true



稍后在应用程序中,我要显示用户信息, code> fillUserViews()在活动B中。但仍旧显示我的旧信息。 p> 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());

$ / code $ / pre

重新启动应用程序在这里没有帮助。但是,如果我注销并再次登录我的用户数据更新。 我认为这与Firebase内部缓存有关。这就是为什么在显示敬酒之后添加了这行 auth.getCurrentUser()。reload();

是否有可能强制更新请求到Firebase?

在此先感谢!

解决方案

wuhuuuu!我发现了一个解决方案:

我正在重新加载用户对象,当我非常需要它,请求成功后,我正在更新我的视图!

  private void fillUserViews(NavigationView navigationView){
final查看headerView = navigationView.getHeaderView(0);
final FirebaseAuth auth = FirebaseAuth.getInstance();
auth.getCurrentUser()
.reload()
.addOnSuccessListener(new OnSuccessListener< Void>(){
$ b $ @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() );
}
});
}


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!

解决方案

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());
               }
            });
   }

这篇关于从Firebase身份验证获取更新的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆