Home > Software design >  in Security Firstore Rules does it possible to only make users to read data depending on field value
in Security Firstore Rules does it possible to only make users to read data depending on field value

Time:03-20

for example in Firstore i have collection called products and every doc has boolen field called isAllow : false

now in Security Firstore Rules How to make users can read only the docs with true value of isAllow field and the same with write .. i read the documentation but couldn't understand exactly what i want

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

CodePudding user response:

As Dharmaraj answered, you can allow only reading of those documents in security rules with:

match /products/{product} {
  allow read: if resource.data.isAllow == true;
}

But keep in mind that security rules are not filters on their own, and instead merely ensure the client doesn't try to read data it isn't permitted to. To meet the security rule above, you'll also need to use a query to read the allowed data:

firebase.firestore()
  .collection("products")
  .where("isAllow", "==", true)

CodePudding user response:

Yes,

match /collection/{document} {
  allow read: if resource.data.isAllow == true;
}

Here resource.data is a map of all of the fields and values stored in the document and the above rule will allow read operation only when isAllow field in the document being accessed is true.

  • Related