我有一个具有以下结构的firebase数据库
我想定义一个数据库规则,其中已认证的用户只能在已认证用户的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));
});
答案 0 :(得分:0)
尝试以下规则阅读订单
{
"rules": {
"orders": {
"$orderId": {
".read": "data.child('userId').val() == auth.uid",
".write": "auth != null"
}
}
}
}