Disable querying collection in Firebase Cloud Firestore with rules

孤街醉人 提交于 2020-05-11 04:27:29

问题


I am using Firebase Cloud Firestore, and I want to modify my rules to restrict users from querying a collection.

This should not be allowed:

firestore().collection("users").get()

But this should be allowed:

firestore().collection("users").doc("someUserId").get()

Currently, my rules look like this:

match /users/{userId} {
    allow read;
}

but this rule allows the "users" collection to be queried.

How can I allow single document gets, but not collection queries?


回答1:


You can break read rules into get and list. Rules for get apply to requests for single documents, and rules for list apply to queries and requests for collections (docs).

match /users/{userId} {

  //signed in users can get individual documents
  allow get: if request.auth.uid != null;

  //no one can query the collection
  allow list: if false;
}



回答2:


Give the following a try. I haven't been able to test it, so apologies if I've mistyped something.

match /users/{userId} {
    allow read: if $(request.auth.uid) == $(userId);
}


来源:https://stackoverflow.com/questions/48659317/disable-querying-collection-in-firebase-cloud-firestore-with-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!