Firebase 规则 - 如果用户的 uid 等于文档的字段值 userId 则允许读取

发布于 2025-01-18 15:46:45 字数 698 浏览 2 评论 0原文

我试图仅在用户的 uid 与 userId 匹配(用红色圈出)时才允许读取硬币的文档。

输入图片此处描述

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    
    match /{document=**} {
        allow read, write: if false;
    }
    
    match /coins/{coinId} {
        allow read: if request.auth != null && resource.data.userId == request.auth.uid;
        allow write: if request.auth != null;
    }
  }
}

当我登录我的帐户时,此规则在控制台中返回错误。并且没有数据显示。 快照侦听器中未捕获的错误:FirebaseError:缺少权限或权限不足。

根据我在此处阅读的所有内容,以下代码似乎应该有效,但事实并非如此。

I am trying to only allow reads coins's documents if the user's uid matches the userId (encircled in red).

enter image description here

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    
    match /{document=**} {
        allow read, write: if false;
    }
    
    match /coins/{coinId} {
        allow read: if request.auth != null && resource.data.userId == request.auth.uid;
        allow write: if request.auth != null;
    }
  }
}

This rule returns an error in the console when I log into my account. And no data is displayed.
Uncaught error in snapshot listener: FirebaseError: Missing or insufficient permissions.

Based on everything I've read here it seems like the following code should work, but it doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-01-25 15:46:45

有同样的问题。

改变:
资源.data.userId == request.auth.uid;

到:
request.resource.data.userId == request.auth.uid;

Had the same problem.

Changed:
resource.data.userId == request.auth.uid;

To:
request.resource.data.userId == request.auth.uid;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文