1

I'm currently using Firestore for the first time and trying to understand the security rules a bit. I now my question is really simple and that I could figure out an answer by doing a bit more research but I wanted to be sure that I am doing the right thing, so I thought it would be better to just ask here.

If I had two collections in Firestore one called "A" and the other "B" what would my security rules have to be if I wanted just authenticated users to read, write, update, delete... in A and everyone to read in B but just authenticated users to write, update, delete... in B.

Edit: Here are the current rules they apply the rules for B to all collections:

service cloud.firestore {
   match /databases/{database}/documents {
      match /{document=**} {
        allow read: if true;
        allow write: if request.auth.uid != null;
      }
   }

}

2
  • 1
    Please put the Firestore rules in the thread. (i’m on my phone so easier to read it directly from here) and i’ll try to help you out. :)
    – Putte
    Commented Feb 24, 2019 at 20:34
  • 1
    firebase.google.com/docs/firestore/security/rules-structure - Here is how you can structure it up. Using the ”match /{}” Something like this: match / {B}
    – Putte
    Commented Feb 24, 2019 at 20:42

1 Answer 1

3

If you look at the documentation on authentication in security rules, you will find these rules:

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to access documents in the "cities" collection
    // only if they are authenticated.
    match /cities/{city} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

Modified for your use-case, that'd be something like:

service cloud.firestore {
  match /databases/{database}/documents {
    match /A/{id} {
      allow read, write: if request.auth.uid != null;
    }
    match /B/{id} {
      allow read;
      allow write: if request.auth.uid != null;
    }
  }
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.