Google cloud firestore 如何仅允许某些用户读取Firestore数据库

Google cloud firestore 如何仅允许某些用户读取Firestore数据库,google-cloud-firestore,Google Cloud Firestore,我正在开发一个基于firestore数据库的小型Web应用程序,我只希望某些用户能够访问(读写)数据。它可能是15或20个UID。我已经在根级别创建了一个名为“白名单”的集合,其中每个文档都有用户的UID和几个带有userID、userName和userEmail的字段 然后我写下了我的规则如下: service cloud.firestore { match /databases/{database}/documents { match /{document=**} {

我正在开发一个基于firestore数据库的小型Web应用程序,我只希望某些用户能够访问(读写)数据。它可能是15或20个UID。我已经在根级别创建了一个名为“白名单”的集合,其中每个文档都有用户的UID和几个带有userID、userName和userEmail的字段

然后我写下了我的规则如下:

service cloud.firestore {   match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null && data.child('whitelist').hasChild(auth.uid);
    }   } }
但是,我已经尝试过与我自己的用户一起查看它是否有效,并且即使白名单集合中有文档,用户也无法阅读任何内容。我做错了什么

// This will check if the user is authenticated and his UID exists 
// in "haveAccess" collection which must be placed in root. You can use the same for ".write"

".read":"(auth !== null && data.child('haveAccess').hasChild(auth.uid))"

您可以在此处查看文档中的规则:

确保语句
data.child('whitelist').hasChild(auth.uid)
返回
true
是非常值得的

同样根据您的需要,您应该使用
资源。数据

resource
变量引用请求的文档,
resource.data
是文档中存储的所有字段和值的映射。有关
资源
变量的更多信息,请参阅

您可以尝试添加
资源

service cloud.firestore {   match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null && resource.data.child('whitelist').hasChild(auth.uid);
    }   } }

您可以将它们插入数据库的集合中,然后在规则中检查该集合中是否存在,或者您可以使用它们的UID创建对象,然后只允许访问UID中包含的对象matches@ErayIsmailov我已经有一个名为users的集合,其中有这些用户。它们中的每一个都是该集合中的一个文档。我该怎么做呢?我相信这个指令是针对实时数据库的,我使用的是不同的Firestore。我曾尝试将其适应Firestore安全规则,但它不起作用。我只是编辑了主要的帖子来通知我所做的事情。谢谢你!