Search code examples
securityfirebaseangularfiregoogle-cloud-firestore

Firebase Firestore - Iterate collection with restricted security rules


In Firestore I defined a rule like this:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, list: if request.data.visibility == "public";
    }
  }
}

Then I want to get all public documents, so I subscribe to the collection using AngularFire:

this.docs = db.collection('documents', ref => ref.where("visibility", "==", 'public')).snapshotChanges();

But this throws:

Missing or insufficient permissions.

Is this supposed to happen? Is it possible to iterate a collection with restricted documents? I'm not a big fan of subcollections, but is that the only way to achieve this?


Solution

  • Solved, I was missing the list rule, and I also didn't add the documents collection. The final rule is this:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /documents/{document=**} {
          allow read, list: if resource.data.visibility == "public"
        }
      }
    }