【问题标题】:Can't read data from Firebase after authentication身份验证后无法从 Firebase 读取数据
【发布时间】:2016-04-22 21:21:12
【问题描述】:

大家好,我是 Firebase 的新手,我正在尝试开发一个简单的聊天应用。 到目前为止,我已经按照 Firebase 文档中的步骤完成了身份验证。

这是我的登录方式

loginUser: function(){
        console.log("Login button");
        var self = this;
        ref.authWithOAuthPopup("github", function(error, authData) {
            if (error) { console.log("Login Failed!", error);} 
            else {
                console.log(authData);
                ref.child("users").child(authData.uid).set({
                    auth : authData.auth,
                    provider : authData.provider,
                    name : authData.github.displayName,
                    imgUrl : authData.github.profileImageURL,
                    token: authData.token
                });
                self.user.name = authData.github.displayName;
                self.user.imgUrl = authData.github.profileImageURL;
                self.user.provider = authData.provider;
                setTimeout(function(){ self.authenticated = true; }, 2000);
                this.getContacts();
            }
        },{ 
            remember : "sessionOnly", 
            scope: "user"
        });

    }

这是 getContacts 方法(我试图控制快照但我什么也没得到)

    getContacts: function(){
        console.log('GET CONTACTS');
        var self = this;
        //retrieving all the user, but for somehow this request doesn't execute
        ref.child('users').once('value',function(snapshot){
            var contacts = snapshot.val();
            console.log(contacts);
            for(var contact  in contacts){
             self.contacts.push({
                id: contact,
                name: contacts[contact].name,
                imgUrl: contacts[contact].imgUrl,
                provider: contacts[contact].provider
             });
            }
        });
    }

这些是安全规则

{
  "rules": {
    "users": {
      "$uid": {
        // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
         ".write": "auth !== null && auth.uid === $uid",
         // grants read access to any user who is logged in with GitHub
         ".read": "auth !== null && auth.provider === 'github'"
       }
     }
 }

不得不提一下,我使用的是 Vuejs

【问题讨论】:

    标签: javascript firebase firebase-security firebase-authentication


    【解决方案1】:

    您正在向特定用户授予访问权限:/users/$uid。但是为了能够对 Firebase 中的节点运行查询,您必须具有对该节点的读取权限。因此 Firebase 拒绝您的查询,因为您无权访问 /users。文档在"rules are not filters" 部分对此进行了介绍。很多人都会遇到这个问题,因为这是关系数据库中非常常见的方法。 rules are not filterspermissions cascade down 是 Firebase 安全模型中两个最常见的缺陷。

    在这种情况下,您可能希望授予所有用户对整个 /users 节点的访问权限,因此您只需将读取权限上移一级:

    {
      "rules": {
       // grants read access to any user who is logged in with GitHub
       ".read": "auth !== null && auth.provider === 'github'"
        "users": {
          "$uid": {
            // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
             ".write": "auth !== null && auth.uid === $uid",
           }
         }
     }
    

    这样每个通过 github 认证的用户,都可以读取所有用户,因此查询不会被拒绝。

    在其他情况下,您可能必须对数据进行不同的建模才能满足所需的安全约束。

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2018-01-07
      • 2020-02-29
      相关资源
      最近更新 更多