Firestore 安全规则权限问题
Firestore Security Rules Permission Issue
我在名为 'inventories' 的 firestore 集合中有以下结构的文档:
{
creator: string,
images: string[],
}
creator 字段是创建文档的用户的 uid。
在我的 firestore 规则部分,我有以下规则:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read, write: if request.auth.id == resource.data.creator;
}
}
}
在我的离子应用程序中,我执行以下操作:
this.inventoryCollection = database.collection<Inventory[]>
('inventories', ref => ref.where('creator', '==', auth.currentUserId));
执行时出现以下错误:
Missing or insufficient permissions.
谁能看出我做错了什么?是我的规则有问题吗?还是我调用 firestore 的代码有问题?
您应该使用 request.auth.uid
而不是 request.auth.id
,请参阅 https://firebase.google.com/docs/reference/rules/rules.firestore.Request#auth
此外,您应该将规则分成两部分 "sub-rules",如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read: if request.auth.id == resource.data.creator;
allow write: if request.auth.id == request.resource.data.creator;
}
}
}
请注意 request.resource
用于 write
规则。
这在以下关于 Firestore 安全规则的官方视频中有很好的解释:https://www.youtube.com/watch?v=eW5MdE3ZcAw
我在名为 'inventories' 的 firestore 集合中有以下结构的文档:
{
creator: string,
images: string[],
}
creator 字段是创建文档的用户的 uid。
在我的 firestore 规则部分,我有以下规则:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read, write: if request.auth.id == resource.data.creator;
}
}
}
在我的离子应用程序中,我执行以下操作:
this.inventoryCollection = database.collection<Inventory[]>
('inventories', ref => ref.where('creator', '==', auth.currentUserId));
执行时出现以下错误:
Missing or insufficient permissions.
谁能看出我做错了什么?是我的规则有问题吗?还是我调用 firestore 的代码有问题?
您应该使用 request.auth.uid
而不是 request.auth.id
,请参阅 https://firebase.google.com/docs/reference/rules/rules.firestore.Request#auth
此外,您应该将规则分成两部分 "sub-rules",如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /inventories/{inventoryid} {
allow read: if request.auth.id == resource.data.creator;
allow write: if request.auth.id == request.resource.data.creator;
}
}
}
请注意 request.resource
用于 write
规则。
这在以下关于 Firestore 安全规则的官方视频中有很好的解释:https://www.youtube.com/watch?v=eW5MdE3ZcAw