Javascript Firebase-如何在不显示用户id的情况下正确设置写入访问权限?
我有以下数据结构:Javascript Firebase-如何在不显示用户id的情况下正确设置写入访问权限?,javascript,json,firebase,authorization,Javascript,Json,Firebase,Authorization,我有以下数据结构: cars: { $uid: { brand: "Brand here", model: "Model here etc" }, $uid: { brand: "Another brand here", model: "Another model here etc" } } 任何人都可以阅读这些数据,比如:firebase.com/cars/uniqueIDHere.json——这是一个公共API firebase
cars: {
$uid: {
brand: "Brand here",
model: "Model here etc"
},
$uid: {
brand: "Another brand here",
model: "Another model here etc"
}
}
任何人都可以阅读这些数据,比如:firebase.com/cars/uniqueIDHere.json——这是一个公共API
firebase.com/cars.json-不可访问
现在的规则设置如下:
"cars": {
"$uid": {
".read": "true",
".write": "auth != null"
}
}
我希望有以下授权规则:只有创建汽车的用户才能编辑/删除它。
现在有了这些规则,我想所有登录的用户都可以这样做,如果他们知道汽车的$uid。
可以制定哪些规则,以便只有所有者才能编写。我知道这样的规则:
"$uid": {
".read": "true",
".write": "auth.uid === $uid"
}
其中$uid是用户的uid,但我不希望这样,因为它将在公共API url中公开
我可以采取什么方法?有什么想法吗?我认为你需要将用户和汽车分开 这样的结构:
root
|
+-- cars
| |
| +-- $carId
| |
| +-- brand: $brand
| |
| +-- model: $model
|
+-- users
| |
| +-- $userId
| |
| +-- cars
| |
| +-- $carId: true
规则如下:
{
"rules": {
"cars": {
"$carId": {
".read": "true",
".write": "auth != null && root.child('users').child(auth.uid).child("cars").child($carId).val() === true"
}
},
"users": {
"$userId": {
".read": "auth != null && auth.uid === $userId",
".write": "auth != null && auth.uid === $userId"
}
}
}
}