数据中存储的数据库安全规则中的“使用”字段

时间:2019-12-22 16:21:47

标签: firebase firebase-realtime-database firebase-security

我有一个具有以下结构的firebase数据库

enter image description here

我想定义一个数据库规则,其中已认证的用户只能在已认证用户的ID等于数据中定义的userId的情况下读取数据。 如何定义此规则?

我尝试定义如下所示的规则,但是id无效。它不允许任何读取操作

{
  "rules": {
    ".read": "auth.uid == root.child('orders').child('userId').val()",
    ".write": "auth != null"
  }
}

这就是我向Firebase的REST api发出GET请求的方式

axios.get(`/orders.json?auth=${firebaseAuthToken}`)
        .then(response => {
            if (response.status === 200) {
                dispatch(fetchOrdersSuccess(Object.values(response.data)));
            }
        })
        .catch(error => {
            dispatch(fetchOrdersError(error.message));
        });

1 个答案:

答案 0 :(得分:0)

尝试以下规则阅读订单

{
  "rules": {
    "orders": {
      "$orderId": {
        ".read": "data.child('userId').val() == auth.uid",
        ".write": "auth != null"
      }
    }
  }
}
相关问题