【问题标题】:How to allow existing users to edit their data only? (Firebase security rules)如何只允许现有用户编辑他们的数据? (Firebase 安全规则)
【发布时间】:2019-09-12 04:41:23
【问题描述】:

我正在编写一个 RSVP 应用程序,用户可以在其中进入网站并输入他们的姓名。我想,如果他们的名字与数据库中的记录匹配,他们有权编辑他们的参加字段。在这种情况下,我对安全性知之甚少。

我在 firebase 文档中找不到我要查找的内容。我主要看到通过用户名和密码进行身份验证,提供完全的读写访问权限,我不想这样做。

{
  "rules": {
    ".read": (if user name matches a record),
    ".write": (if user name matches a record, only can edit)
  }
}

我不确定在授予用户“写入”访问权限时如何验证我的用户身份,以将其限制为只能编辑数据。我不希望任何恶意用户删除我的数据库。

【问题讨论】:

    标签: firebase firebase-realtime-database firebase-authentication firebase-security


    【解决方案1】:

    只需匹配路径中的用户名并将写入字段设置为匹配名称即可:

    match /users/{username}{
        allow read: if request.auth.username != null
        allow write: if request.auth.username == username
    }
    

    您将应用程序设置为无需任何登录即可工作,并将输入的用户名作为身份验证对象auth = { username: "" } 的用户名字段值。 Firebase 会将通过路径 /users/{username} 提供的用户名匹配为请求的用户名,并相应地允许读/写访问。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2015-11-22
      • 2018-12-08
      • 2016-02-26
      • 1970-01-01
      • 2018-12-26
      • 2020-07-11
      • 2019-09-24
      • 2017-06-13
      相关资源
      最近更新 更多