【问题标题】:How to get user information from user node using authentication UId in firebase for android?如何使用Android Firebase中的身份验证UId从用户节点获取用户信息?
【发布时间】:2018-04-12 10:51:19
【问题描述】:

我使用 Firebase 电子邮件/密码身份验证进行登录。我使用了一个用户节点来存储用户数据。我可以使用共享首选项将身份验证 Uid 传递给不同的活动。如何使用此 UId 获取用户信息,如姓名、电话等?

注意:用户信息存储在用户节点中,UId来自身份验证。

我试过下面的方法,但是UName、UMobile、UEmail都显示空值。我可以正确获取 UId。

 FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser() ;
 String UId = currentUser.getUid();
 String UName = currentUser.getDisplayName();
 String UMobile = currentUser.getPhoneNumber();
 String UEmail = currentUser.getEmail();

【问题讨论】:

  • 你实现了onAuthStateChanged监听器吗?
  • 我需要在哪里实现?
  • 你有implement什么吗?
  • 我刚刚在 auth.signInWithEmailAndPassword(email,password) 中使用了上面的代码
  • 你能发一下code吗?

标签: android firebase firebase-realtime-database sharedpreferences


【解决方案1】:

请看一下这个指南,注意 FirebaseAuth.AuthStateListener()

https://www.androidhive.info/2016/06/android-getting-started-firebase-simple-login-registration-auth/

如果您使用 UID 和其他自定义字段将用户存储在节点中,那么您将无法使用 FirebaseAuth 获取它们。 您需要使用此 UID 查询保存用户的树并解析用户数据快照。 只能使用 FirebaseAuth 检索 UID。

【讨论】:

  • 这里我也使用 UId 作为用户在数据库中的唯一 id。现在我想获取用户详细信息,如姓名、电话等,并将其存储为全局共享偏好。
  • 如果有,如何查询?
【解决方案2】:

当你存储用户数据时,用 uid 存储。就像 json 对象一样。 uid 作为键,用户数据作为值。然后您可以使用此 uID 获取 firebaseDatabasereference。

 {
"users": {
"uid_of_first_user": {
  "username": "usernam",
  "email": "bobtony"
},
 "uid_of_second_user": {
  "username": "usernam",
  "email": "bobtony"
},

}}

使用以下代码获取值

  FirebaseDatabase database = FirebaseDatabase.getInstance();
  DatabaseReference myRef = database.getReference("users/uid_of_first_user");

   // Read from the database
   myRef.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
    // This method is called once with the initial value and again
    // whenever data at this location is updated.
    User value = dataSnapshot.getValue(User.class);
    Log.d(TAG, "Value is: " + value);
  }

   @Override
    public void onCancelled(DatabaseError error) {
    // Failed to read value
    Log.w(TAG, "Failed to read value.", error.toException());
}

});

创建 User.class。每个变量名称必须与 firebase 中的相同。并且不要忘记创建默认构造函数。就像下面

  public class User {
     String username;
     String email;

     public User() {
     }
     public User(String username, String email) {
     this.username = username;
     this.email = email;
     }

   }

如果您想获取所有用户数据。按照下面的代码。

  FirebaseDatabase database = FirebaseDatabase.getInstance();
  DatabaseReference myRef = database.getReference("users");

  myRef.addValueEventListener(new ValueEventListener() {
  @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
        // TODO: handle the post
          User value = userSnapshot.getValue(User.class);
    }
 }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    // Getting Post failed, log a message
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    // ...
  }
 });

【讨论】:

    【解决方案3】:

    覆盖 onAuthStateChanged 监听器:

    mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    
                if (firebaseAuth.getCurrentUser() != null) {
    
                    //show_snackbar("logged on: " + firebaseAuth.getCurrentUser().getEmail(), Snackbar.LENGTH_SHORT);
    
                    Uri img = firebaseAuth.getCurrentUser().getPhotoUrl();
                    Picasso.with(context).load(img).into(account_image);
    
                    account_name.setText(firebaseAuth.getCurrentUser().getDisplayName());
    
                }
            }
        };
    

    【讨论】:

      【解决方案4】:

      检查此代码。它不会运行,但如果您了解 Firebase,您会很容易理解它。 FriendlyChat。阅读 cmets。或者直接下载完整代码,获取json文件自己测试。

      【讨论】:

        猜你喜欢
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        • 2020-12-04
        • 2017-01-03
        • 1970-01-01
        • 2018-04-08
        • 2019-05-23
        • 1970-01-01
        相关资源
        最近更新 更多