Javascript 如何确保Firebase数据库中用户和管理员的访问安全?

Javascript 如何确保Firebase数据库中用户和管理员的访问安全?,javascript,firebase-realtime-database,firebase-security,Javascript,Firebase Realtime Database,Firebase Security,我正在使用Redux Saga作为中间件。我正在通过查询将参数传递给Firebase数据库,但无法在数据库端访问它 查询::: database.ref("workouts") .child(userId) .once("value") .then(snapshot => { console.log("onSuccess:", snapshot.ref, snapshot.val()); resolve(snapshot.val()); }) .catch(function(erro

我正在使用Redux Saga作为中间件。我正在通过查询将参数传递给Firebase数据库,但无法在数据库端访问它

查询:::

database.ref("workouts")
.child(userId)
.once("value")
.then(snapshot => {
 console.log("onSuccess:", snapshot.ref, snapshot.val());
 resolve(snapshot.val());
 })
.catch(function(error) {
 console.log("Error fetching document: ", error);
 reject(error);
 });
UserId是我从localStorage获取的一个值,并使用.childuserId通过查询发送到数据库

查询:::对于管理员

database.ref("workouts")
.once("value")
.then(snapshot => {
 console.log("onSuccess:", snapshot.ref, snapshot.val());
 resolve(snapshot.val());
 })
.catch(function(error) {
 console.log("Error fetching document: ", error);
 reject(error);
 });
数据库中的规则::

{
"rules": {
         "workouts": {
          // grants write access to the owner of this user account || the user role is equal to  admin
          // grants read access to the owner of this user account || the user role is equal to  admin
".read":"(data.exists() && auth.uid != null && data.child(auth.uid).exists()) ||root.child('users').child(auth.uid).child('role').val() == 'admin'",
".write":"data.exists() ||root.child('users').child(auth.uid).child('role').val() == 'admin'"
                     }
         }
}

我尝试了[query.equalTo]和[data.childauth.uid.val]方法来访问该值,但没有得到任何结果

用于训练的JSON:

"workouts" : {
"6OiasllKwVSjjRfrCarMAjhkKAH2" : {
  "-LD3nNIKw9Yk3HcoAL0-" : {
         "exercises" : [ {
          "muscleGroup" : "Chest",
          "name" : "Incline Dumbbell Fly",
          "sets" : [ 0, 0, 0, 0, 0 ],
          "type" : "reps"
        } ],
        "name" : "Force Set",
        "reps" : [ "5", "5", "5", "5", "5" ],
        "type" : "Weights"
       }]
    },
    "workoutName" : "My Test workout"
  }
用户的JSON:

 "users" : {
     "6OiasllKwVSjjRfrCarMAjhkKAH2" : {
     "email" : "testuser@gmail.com",
     "role" : "user",
     "uid" : "6OiasllKwVSjjRfrCarMAjhkKAH2"
       }
     }
非常感谢您的任何帮助

事先非常感谢你


编辑:::添加了对管理员的查询。如果是管理员,我想获取集合中所有可用的数据。

我想我知道出了什么问题。您的JSON似乎在/workouts/$uid下包含用户的所有训练。您的规则尝试让用户访问所有的/训练,而不仅仅是他们自己的

解决方案是将规则向下移动一级到树中:

{
  "rules": {
    "workouts": {
      // grants access to the owner of this user account || the user role is equal to  admin
      "$uid": {
        ".read":"auth.uid == $uid || root.child('users').child(auth.uid).child('role').val() == 'admin'",
      },
      ".write":"data.exists() || root.child('users').child(auth.uid).child('role').val() == 'admin'"
    }
  }
}
另请参见,其中有一个很好的简单示例

更新:如果您希望允许管理员阅读/workouts,并且每个用户都能够在/workouts/$uid下阅读自己的训练,那么您需要以下规则:

{
  "rules": {
    "workouts": {
      // grants access to the owner of this user account
      "$uid": {
        "read": "auth.uid == $uid",
      },
      // grants access to the admin
      ".read": "root.child('users').child(auth.uid).child('role').val() == 'admin'",
      ".write": "data.exists() || root.child('users').child(auth.uid).child('role').val() == 'admin'"
    }
  }
}

我尝试了[query.equalTo]和[data.childauth.uid.val]方法来访问该值,但没有得到任何结果。这是行不通的,因为您没有执行查询。您是否可以更新您的问题,将/workouts/$uid和/users/$uid下的JSON作为文本包含,而不包含屏幕截图?你可以通过点击你网站上的导出JSON链接来获得这个信息。首先感谢Frank给你宝贵的时间。我也试过同样的方法,但这会在OR条件下破裂。因为我还需要提供对管理员的访问权限。嗨,Frank,除了使用“$variables”之外,我们还有什么方法可以提取在“child”中传递的值吗?@SumitKhatri你不能保持管理员规则与你的问题相同,并使用用户规则与此答案相同吗?.read条件应该完全符合你的要求。什么中断?请检查更新的问题。我还添加了对admin的查询。