我想写一个这样的规则:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId}/{document=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
也就是说,我想允许,如果该用户是经过验证的用户的所有读取和写入到所有用户的数据的操作。
不幸的是不起作用。我得到一个错误,指定我没有权限访问数据。
此代码解决了这个问题:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
match /{document=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
}
我想这是因为你需要访问权限授予/users/{userId}
,以及/users/{userId}/{anyDoc=**}
。
另一种常见的模式是,以确保用户只能读取和写入自己的数据:
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
}
}
如果应用程序使用火力地堡身份验证,则request.auth变量包含客户端请求数据的验证信息。
请注意,如果您已经在数据库中做了一个“用户”表,并与已知您的应用程序的用户填充它这仅适用(可能是从火力的用户部门认证复制/在Web控制台的用户)。
AFAICS你不能指公司的FireStore身份验证的用户表这种方式。我发现这个缺乏信息非常混乱,因为所有的实例和文档的FireStore让你相信,你可以通过访问该Web控制台创建的用户这种方式,试图从一个用户表中读取时,总是导致一个“访问被拒绝”的消息...