【问题标题】:How to list users information from firebase on android?如何在android上列出来自firebase的用户信息?
【发布时间】:2017-08-10 14:00:31
【问题描述】:

我正在开发一个 Android 项目。我的火力库中有一个用户节点,我想列出一些信息,但我不想被读取 uid。我采取了措施,但我没有阅读姓名或身份等信息。我该怎么做? 我的数据库规则;

"rules": {
    "users": {
           "$uid": {
               ".read": "$uid === auth.uid",
                  ".write": "$uid!=null && $uid === auth.uid",
            "name-surname": {
              ".read": true,
                ".validate": "newData.isString() && newData.val().length < 30"
            },
            "status": {
              ".read": true,
                ".validate": "newData.isString()&& newData.val().length < 75"
            },
          "website": {
            ".read": true,
                ".validate": "newData.isString()&& newData.val().length < 80"
            },
          "linkedin": {
            ".read": true,
                ".validate": "newData.isString()&& newData.val().length < 80"
            },
          "github": {
            ".read": true,
                ".validate": "newData.isString()&& newData.val().length < 80"
            }
           }
    }
}

我的代码部分;

 DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
   final MembersInfo member = new MembersInfo();
    mDatabase.child("users").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Is better to use a List, because you don't know the size
            // of the iterator returned by dataSnapshot.getChildren() to
            // initialize the array

                    String nameSurname = dataSnapshot.child("uid").child("name-surname").getValue(String.class);
            Toast.makeText(MainActivity.this, nameSurname, Toast.LENGTH_SHORT).show();
                    member.NameSurname=nameSurname;

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
        }
    });

【问题讨论】:

  • 您不想读取 uid?
  • 我的意思是任何人都无法访问任何 uid。我认为状态会很危险。

标签: android firebase firebase-realtime-database


【解决方案1】:

知道用户的 UID不是安全风险。在这里查看我的解释:Firebase - Is auth.uid a shared secret?

也就是说,有很多充分的理由只在您的应用中公开一部分用户配置文件。为此,您必须将子集分成单独的顶级列表。例如,要允许仅包含用户名的列表而不共享其他信息:

usernames
  "209103": "Frank van Puffelen"
  "7174606": "oguzkaganeren"

为了能够阅读此列表,您需要在/usernames 上授予访问权限。所以这仍然会共享 UID。

更多示例,请参见:

【讨论】:

  • 感谢您的精彩回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多