【问题标题】:Firebase Firestore: Insufficient permissions to writeFirebase Firestore:写入权限不足
【发布时间】:2017-12-18 15:33:13
【问题描述】:

我正在尝试写一个文档,但是得到了

权限缺失或不足。

我的规则如下:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid == resource.data.uid;
    }
  }
}

我正在努力写作

const accountBalanceRef = db.collection('accountBalances').doc()
accountBalanceRef.set({
    type: s.type,
    accountName: s.accountName,
    startingBalance: s.startingBalance,
    endingBalance: s.endingBalance,
    statementYear: s.year,
    statementMonth: s.monthNumber,
    createdAt: now,
    uid: this.props.uid
})

怎么了? this.props.uid 是我的用户名

已解决

在@Bob Snyder 的帮助下,我用以下规则解决了这个问题

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if request.auth.uid == resource.data.uid;
      allow write: if request.auth.uid == request.resource.data.uid;
    }
  }
}

我需要有不同的读写规则。我认为这是因为阅读,我没有设置request.resource.data.uid

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    在 Firestore 安全规则中,resource.data 是数据库中文档的值。您的规则不允许创建 accountBalance 文档,因为 resource.data 为空。

    如果您希望您的规则要求 accountBalance 中的 uid 字段等于授权用户的 uid,则该规则应使用 request.resource.data.uid

    allow read, write: if request.auth.uid == request.resource.data.uid;
    

    request.resource 的文档说明:

    [request] 资源变量包含有关文档的数据和元数据 被写。它与resource 变量密切相关,后者 包含请求路径中的当前文档,而不是 正在编写的文档。

    【讨论】:

    • 所以要让它工作,每次我想写的时候我都需要提供uid
    • 您的帖子没有明确说明您希望规则强制执行什么行为。你能解释更多吗?也许您需要一条规则用于create,而另一条规则用于“更新”和“读取”。除了readwrite,还有more request methods
    • 由于某种原因,它仍然无法使用缺少权限错误。有什么方法可以调试吗?只要记录的 uid 与他们的 uid 匹配,用户就可以随时写入所需的结果。用户只能更新他们的对象
    • 没有我知道的调试工具。 Firestore 没有像 RealTime DB 这样的规则模拟器。听起来this.props.uid 不是登录用户的uid。在尝试写入之前尝试同时记录两者。
    • 用 OP 中的规则解决。我认为的问题是阅读,request.resource.data.uid 没有设置?所以我需要改用resource
    猜你喜欢
    • 2022-01-25
    • 2020-02-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2019-02-26
    • 2018-09-12
    • 2018-03-20
    相关资源
    最近更新 更多