【问题标题】:How to set one user as admin in firebase?如何在firebase中将一个用户设置为管理员?
【发布时间】:2020-02-10 15:45:00
【问题描述】:

我有用户数据库,我希望每个经过身份验证的用户都可以在其中读取和更新自己的数据。我还想设置一个管理员用户,可以读取和更新数据库中所有用户的数据。

我试过这样做 - How to only allow one admin user to write Firebase Database?

但效果不太好。

我的数据库结构是:

- Users
   -key1
     -email: xyz
   -key2
     -email: abc

这是我尝试过的。在这里,我在 firebase auth 中添加了管理员用户,他的身份验证 ID 是 - s94ZwBcP 但是,我在数据库中没有该用户的任何密钥(因为我不需要它)。

"rules": {
     "users":{
       "$uid":{
         ".read" : "$uid == auth.uid || auth.uid === 's94ZwBcP'", 
         ".write": "$uid == auth.uid || auth.uid === 's94ZwBcP'"
      }
     }

【问题讨论】:

标签: firebase-realtime-database firebase-security


【解决方案1】:

我的猜测是您正在尝试从/users 读取。由于/users 上没有定义.read 规则,因此没有人可以读取该节点。

要允许管理员获取(和修改)所有用户的列表,请将您的规则修改为:

"rules": {
  "users":{
    ".read" : "auth.uid === 's94ZwBcP'", 
    ".write": "auth.uid === 's94ZwBcP'",
    "$uid":{
      ".read" : "$uid == auth.uid", 
      ".write": "$uid == auth.uid"
    }
  }
}

【讨论】:

  • 非常感谢。没有意识到 uid 是用于键的,而用户是一般的。
猜你喜欢
  • 2017-07-10
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 2022-12-03
  • 2019-06-03
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多