简体   繁体   English

不使用 Firebase 数据库规则获取响应数据

[英]do not get response data with firebase database rules

I do not get any return from firebase realtime database after adding a rule for my table of quotes.在为我的报价表添加规则后,我没有从 firebase 实时数据库获得任何回报。 I expect all quotes to be returned where the auth.uid matches the owner value of the quote.我希望在 auth.uid 与报价的所有者值匹配的情况下返回所有报价。 However i get nothing.但是我一无所获。

Can anyone hep me out?有人可以帮我吗?

The rules are specified as:规则指定为:

{
  "rules": {
    "users":{
        ".read": "auth.uid != null",
        ".write": "auth.uid != null"     
    },
    "quotes":{
      "$uid":{
        ".read": "data.child('owner').val() === auth.uid",
      }
    }
  }
}

the quotes table is structured as follows:报价表的结构如下:

{
  "1ec658d2-a7cb-45b8-8d9b-9c2a6783365d" : {
    "dateCreated" : "2019-12-02T16:06:50+01:00",
    "owner" : "DVRVSpeOXQV6wAmHAdpAe6iPQ5i2",
    "ownerName" : "testOost",
    "projectName" : "testProject1"
  },
  "96549b51-6356-4c37-a388-592561394d1a" : {
    "dateCreated" : "2019-09-25T14:58:13+02:00",
    "owner" : "xZBFCq4ho3V2G8dZTvK7RjsnTr43",
    "ownerName" : "timcastelijn",
    "projectName" : "testProject2"
  }
}

my code to access the data is我访问数据的代码是

this.props.firebase.db.ref('quotes/').on('value', snapshot => {
  const quotesObject = snapshot.val();

  /* quotesObject is handled here */

});

UPDATE:SOLVED更新:已解决

the issue was solved with @frank-van-puffelen 's solution.该问题已通过 @frank-van-puffelen 的解决方案解决。

the new rules:新规则:

{
  "rules": {
    "users":{
        ".read": "auth.uid != null",
        ".write": "auth.uid != null"     
    },
    "quotes":{
      ".read": "auth.uid != null &&
                query.orderByChild == 'owner' &&
                query.equalTo == auth.uid" // restrict basket access to owner of quote
      }
    }
  }
}

the new snippet:新片段:

this.props.firebase.db.ref('quotes/').orderByChild("owner")
             .equalTo(this.authUser.uid)
             .on("value", snapshot => {

  const quotesObject = snapshot.val();

  ....

});

} }

Your code tries to read from /quotes , but your rules allow no-one to read from /quotes .您的代码尝试从/quotes读取,但您的规则不允许任何人从/quotes读取。 So the rules reject the read operation.所以规则拒绝读操作。

Keep in mind:记住:

  • Rules don't filter data, instead they merely enforce the access rules.规则不会过滤数据,而只是强制执行访问规则。
  • If you want to only allow a user to read their own data, your code and rules need to work together to allow that.如果您只想允许用户读取他们自己的数据,则您的代码和规则需要协同工作以允许这样做。

In your current use-case you can simply modify the code to only read the node for that specific user:在您当前的用例中,您可以简单地修改代码以仅读取该特定用户的节点:

var uid = firebase.auth().currentUser.uid;
this.props.firebase.db.ref('quotes/'+uid).on('value', snapshot => {
  const quotesObject = snapshot.val();

  /* quotesObject is handled here */

});

In other cases, you'll want to use a query in your code, and then validate that query in the rules.在其他情况下,您需要在代码中使用查询,然后在规则中验证该查询。

For more on these see:有关这些的更多信息,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM