当用户第一次登录时,我还需要调用一个在我的firestore用户集合中创建文档的函数来存储他们的配置文件数据。使用Web SDK。
(我以前使用具有firebase功能的新用户触发事件,但是等待冷函数旋转太慢了)。
安全规则要求
需要确保用户只能在文档ID与其用户ID相同时创建文档(以防止用户创建其他文档)。需要确保此doc尚不存在。
尝试 - 在模拟器中工作,而不是IRL
这些测试通过模拟器,但不是IRL。
// Allow users to create a doc if the doc ID == their user id
allow create: if path("/databases/" + database + "/documents/users/" + request.auth.uid) == request.path;
要么
allow create: if /databases/$(database)/documents/users/$(request.auth.uid) == request.resource['__name__']
也试过这个(再次,在模拟器中工作,但不是IRL)
match /users/{userId} {
// Allow users to read their own profile
allow create: if request.auth.uid == userId;
}
所以我想出了如何以解决方法的方式做到这一点。我还有一些额外的写入/更新条件,阻止用户更改其权限级别。这是出于某种原因,防止任何“创造”发生。所以我不得不在create和write / update规则中镜像相同的条件。出于某种原因,这是必要的。
第一部分,用于创建规则
第二部分 - 读取,更新,写入
规则
service cloud.firestore {
match /databases/{database}/documents {
// Allow users to create documents in the user's collection
match /users/{document=**} {
allow create: if request.auth.uid != null &&
!("admin" in getAfter(/databases/$(database)/documents/users/$(request.auth.uid)).data);
}
// Allow users to read, write, update documents that have the same ID as their user id
match /users/{userId} {
// Allow users to read their own profile (doc id same as user id)
allow read: if request.auth.uid == userId;
// Allow users to write / update their own profile as long as no "admin" field is trying to be added or created
allow write, update: if request.auth.uid == userId &&
!("admin" in getAfter(/databases/$(database)/documents/users/$(request.auth.uid)).data);
}
}
}
PS这根本不直观,所以如果有人有更好的解决方法,请发布。此外,我真的希望一旦firestore 1.0出来,它将带来一些规则和规则文档的巨大改进。
有点晚了,但我设法调整你的一个可能的解决方案并让它工作:
allow create: if path("/databases/(default)/documents/users/" + request.auth.uid) == request.path;
只需用database
替换(default)
变量。是的,不是花哨的......