简体   繁体   English

Cloud Firestore 规则问题

[英]Cloud Firestore rules issue

I have an all app which I can dynamically enter things to a list, which are stored in a database in cloud firestore, each item has a name and a userId.我有一个 all 应用程序,我可以动态地将内容输入到列表中,这些内容存储在 cloud firestore 的数据库中,每个项目都有一个名称和一个 userId。 In my App I have all the authentication done, everything works wonderfully, every time I enter something to the list, each item has the userId of the respective user.在我的应用程序中,我完成了所有身份验证,一切正常,每次我在列表中输入内容时,每个项目都有相应用户的 userId。 The problem is that I am in the final stage where I want each user to have their own list, and not be able to access another user's list and vice versa.问题是我正处于最后阶段,我希望每个用户都有自己的列表,并且无法访问另一个用户的列表,反之亦然。 So I am changing the database rules but i ran into problems.所以我正在更改数据库规则,但遇到了问题。 When entering things to my list, these things are added to the database, with their respective name and user id, but they do not appear in the application, It doesn't show me the items in the app.在我的列表中输入东西时,这些东西会以各自的名称和用户 ID 添加到数据库中,但它们不会出现在应用程序中,也不会显示应用程序中的项目。 but the items exist in the database.但这些项目存在于数据库中。

UPDATE: SOLVED!更新:已解决!

There is a problem with the function matchesUser() in the security rule.安全规则中的 function matchesUser()存在问题。 You are passing an incorrect value request.resource.data to the function.您将错误的值request.resource.data传递给 function。 Instead of that you have to pass resource.data .而不是你必须通过resource.data The request.resource.data variable contains the future state of the document and resource.data contains current state of the document. request.resource.data变量包含文档的未来 state, resource.data包含文档的当前 state。

The code should be代码应该是

allow read: if authed() && matchesUser(resource.data);

There is some mistake with your code.您的代码有一些错误。 According to your code, you trying to access the whole document from the collection shows regardless of the user.根据您的代码,无论用户如何,您都试图从集合shows访问整个文档。 This will be clearly denied by the firebase security rule.这将被 firebase 安全规则明确拒绝。 According to rule an user can only access their own data and remember that firebase security rules are not filters.根据规则,用户只能访问自己的数据并记住 firebase 安全规则不是过滤器。

So the code should be所以代码应该是

db.collection('shows').where('userId','==',uid_of_the_user)
.orderBy('timestamp').onSnapshot((snapshot)=>...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM