【问题标题】:Firebase - how to set properly writing access without showing the user's id?Firebase - 如何在不显示用户 ID 的情况下正确设置写入权限?
【发布时间】:2017-07-31 14:49:39
【问题描述】:

我有以下数据结构:

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 中公开 我可以采取什么方法?有什么想法吗?

【问题讨论】:

    标签: javascript json firebase authorization


    【解决方案1】:

    我认为您需要将用户汽车分开。

    这样的结构:

    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"
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-22
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多