Isurendrasingh
Isurendrasingh

Reputation: 432

Firebase firestore security rules for public and private collections

I have 2 collections subscribers and posts. I have setup the security rules to be read: write when authenticated. But i need the subscribers collection to write the data without authentication and in posts collection to check the authentication and then write How to achieve this?

Upvotes: 4

Views: 1837

Answers (2)

Sushant Somani
Sushant Somani

Reputation: 1500

Set firestore rules and you can test them in the firebase simulator as well.

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

The answer is quite specific to the words of your question that you want to write the data when authenticated for posts collection. I've considered that reading data is still open to all.

Upvotes: 3

Chris Edgington
Chris Edgington

Reputation: 3236

Something like this should do it -

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

Upvotes: 0

Related Questions