How to allow access to a document in Firestore, knowing its id?

  • 0
    Good day!

    It is necessary to implement a chat on firestore between two anonymous users. There is a chats collection with a list of documents, you need to deny access to get a list of all documents in the collection, and allow access to the document if the user knows its identifier.

    Example:
    Such a request should cause an error for the user (permission denied)
    firebase.firestore (). collection ("chats"). get ()

    Such a request should return all the fields of the document
    firebase.firestore().collection("chats").doc("fK3ddutEpD2qQqRMXNW5").get()


    those. you need to provide access to the document if you know its identifier
    JavaScript Aden Haley, May 2, 2020

  • 1 Answers
  • 0
    Found the answer:



    You can split the reading rules and the list. Fetch rules apply to individual document requests, and list rules apply to collection requests and requests ( 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;
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!