【发布时间】:2018-10-04 08:06:01
【问题描述】:
我有 2 个集合 subscribers 和 posts。我已将安全规则设置为read: write when authenticated。但是我需要subscribers 集合来写入数据而无需身份验证,并在posts 集合中检查身份验证,然后编写如何实现这一点?
【问题讨论】:
标签: firebase google-cloud-firestore firebase-security
我有 2 个集合 subscribers 和 posts。我已将安全规则设置为read: write when authenticated。但是我需要subscribers 集合来写入数据而无需身份验证,并在posts 集合中检查身份验证,然后编写如何实现这一点?
【问题讨论】:
标签: firebase google-cloud-firestore firebase-security
设置 firestore 规则,您也可以在 firebase 模拟器中对其进行测试。
service cloud.firestore {
match /databases/{database}/documents {
match /subscribers/{document=**} {
allow read, write : if true;
}
match /posts/{document=**} {
allow read : if true;
allow write: if request.auth.uid != null;
}
}
}
答案非常具体到您想要在为posts 集合进行身份验证时写入数据的问题。我考虑过,读取数据还是对所有人开放的。
【讨论】:
subscribers 成为 write: if true。我是否必须写 `match /posts/{document=**} { 允许阅读:如果为真;允许写入:如果 request.auth.uid != null; }` 这对所有其他集合?
应该这样做 -
service cloud.firestore {
match /databases/{database}/documents {
match /subscribers/{document=**} {
allow read, write;
}
match /posts/{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
【讨论】: