jmapps9
jmapps9

Reputation: 87

Firebase security rules for collections in collection

I am trying to set a firebase security rules for all collections in one collection with Firebase Firestore. I have a collection named Chats inside of it I have two collection one is thread and the other is users. All the documents ids are random.

This is how my Firebase Data Looks Like:

enter image description here enter image description here

I am trying to set the security for all the chats & thread & users. Because chats includes the thread collection and the user collection but when setting the rule below: it does not work properly: Missing or insufficient permissions.

// Chats can be read & written by all users
  match /Chats/{document} {
    allow read, create, update, delete, write: if true
  }

I also tried:

   match /Chats/{document}/thread/{document1} {
     allow read, create, update, delete, write: if true
    }


   match /Chats/{document}/users/{document2} {
     allow read, create, update, delete, write: if true
    } 

But it did not work, so if someone can please help me with it.

Upvotes: 0

Views: 1024

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

Your current rules only matches documents in the /Chats collection:

match /Chats/{userId} 

To make it also match the subcollections , it'd have to be:

match /Chats/{document=**} {

Also see the Firebase documentation on securing hierarchical data, specifically the section on recursive wildcards.

Upvotes: 1

Related Questions