【问题标题】:Cloud firestore security rule allow creation only by authenticated userCloud Firestore 安全规则仅允许由经过身份验证的用户创建
【发布时间】:2021-09-06 18:43:40
【问题描述】:

我有一个帖子集合,其中包含这种结构的帖子文档:

{
 title: "test title"
 ...
 authorUid: "testuid"
}

我想添加一个安全规则,它只允许具有 uid 的经过身份验证的用户将具有相同 authoUid 的文档添加到此集合中。

我试过这个 -


match /posts/{postId} {
  allow read: if true;
  allow create: if signedInUserIsPostAuthor();
}

function signedInUserIsPostAuthor(){
   return request.auth.uid == request.resource.data.authorUid;
}

但这似乎失败了。我假设经过身份验证的用户(request.auth.uid)应该等于写入之前的文档(request.resource.data)。

但这不起作用。任何线索我错过了什么?

【问题讨论】:

  • 您能否编辑您的问题以包含失败的(最少)实际代码?还请确保代码证明符合规则,例如:登录当前用户,并且您将其 UID 写入authorUid

标签: firebase google-cloud-firestore firebase-security


【解决方案1】:

我刚刚运行了您的代码,它运行良好。我附上了截图。

可能的解决方案。

  1. 确认 authorUid 上的拼写错误
  2. 打印作者Uid
  3. 打印 currentUser.uid

Firebase 是说上面的 2 和 3 没有相同的值。我可以确认的是你的规则是正确的。

我假设经过身份验证的用户 (request.auth.uid) 应该等于写入前的文档 (request.resource.data)。

是的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2018-04-04
    • 2021-07-05
    • 2020-01-01
    • 2020-03-06
    • 2021-07-23
    相关资源
    最近更新 更多