Firebase Firestore Rules Subcollections

Viewed 25

I have this React Native app that closely resembles the TikTok app, so in my Main Feed Component there is an overlay with the like button and avatar. Here is my issue: When I go to press the heart, firestore throws a warning
console warning from Firebase
console warning from Firebase and rejects the Promise. The original contestSubmission(the video(parent component) that the overlay resides in/on is owned by the content creator. We are trying to allow any authenticated user to write to the owners Likes collection and the Likes' sub collection usersThatLiked[].

Here are a few of the firestore rules that I have tried so far.

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /user/{uid} {
      allow read: if true;
      allow write: if request.auth.uid == uid
    }
     match /rockContestSubmissions/{document} {
      allow read: if true;
      allow write: if request.auth.uid == request.resource.data.creator
    }
      match /popContestSubmissions/{document} {
      allow read: if true;
      allow write: if request.auth.uid == request.resource.data.creator
    }
     match /hiphopContestSubmissions/{document} {
      allow read: if true;
      allow write: if request.auth.uid == request.resource.data.creator
    }
     match /countryContestSubmissions/{document} {
      allow read: if true;
      allow write: if request.auth.uid == request.resource.data.creator
    }
      match /mainFeed/{document} {
      allow read: if true;
      allow write: if request.auth.uid == request.resource.data.creator
    }
    match /rockContestSubmissions/{document}/{likes}/{usersThatLiked} {
      allow read: if true;
      allow write: if request.auth.uid
    }
      match /popContestSubmissions/{document}/{likes}/{usersThatLiked} {
      allow read: if true;
      allow write: if request.auth.uid 
     match /hiphopContestSubmissions/{document}/{likes}/{usersThatLiked} {
      allow read: if true;
      allow write: if request.auth.uid 
    }
     match /countryContestSubmissions/{document}/{likes}/{usersThatLiked} {
      allow read: if true;
      allow write: if request.auth.uid 
    }
      match /mainFeed/{document}/{likes}/{usersThatLiked} {
      allow read: if true;
      allow write: if request.auth.uid 
    }
  }
}
}

and

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /user/{uid} {
      allow read: if true;
      allow write, delete: if request.auth.uid == uid
    }
     match /rockContestSubmissions/{document} {
      allow read: if true;
      allow write, delete: if request.auth.uid == request.resource.data.creator
    }
      match /popContestSubmissions/{document} {
      allow read: if true;
      allow write, delete: if request.auth.uid == request.resource.data.creator
    }
     match /hiphopContestSubmissions/{document} {
      allow read: if true;
      allow write, delete: if request.auth.uid == request.resource.data.creator
    }
     match /countryContestSubmissions/{document} {
      allow read: if true;
      allow write, delete: if request.auth.uid == request.resource.data.creator
    }
      match /mainFeed/{document} {
      allow read: if true;
      allow write, delete: if request.auth.uid == request.resource.data.creator
    }
    match /rockContestSubmissions/{document}/{likes=**} {
      allow read: if true;
      allow write, delete: if request.auth.uid 
    }
      match /popContestSubmissions/{document}/{likes=**} {
      allow read: if true;
      allow write, delete: if request.auth.uid
    }
     match /hiphopContestSubmissions/{document}/{likes=**} {
      allow read: if true;
      allow write, delete: if request.auth.uid 
      }
     match /countryContestSubmissions/{document}/{likes=**} {
      allow read: if true;
      allow write, delete: if request.auth.uid 
    }
      match /mainFeed/{document}/{likes=**} {
      allow read: if true;
      allow write, delete: if request.auth.uid
    }
  }
}

and, lastly

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /user/{uid} {
      allow read: if true;
      allow write: if request.auth.uid == uid
    }
     match /rockContestSubmissions/{document=**} {
      allow read: if true;
      allow write: if request.auth.uid 
    }
      match /popContestSubmissions/{document=**}{
      allow read: if true;
      allow write: if request.auth.uid
    }
     match /hiphopContestSubmissions/{document=**} {
      allow read: if true;
      allow write: if request.auth.uid
    }
     match /countryContestSubmissions/{document=**} {
      allow read: if true;
      allow write: if request.auth.uid 
    }
      match /mainFeed/{document=**} {
      allow read: if true;
      allow write: if request.auth.uid
    }
  }
}

here is what that data is structured like

CloudFirestore data structure
CloudFirestore data structure

Here are the rules that I have tried:

1 Answers

Using wildcards will apply rules to the given collection and all sub collections.

For example,


match /users/123ABC {
  // this will apply to the specific
  // doc in the users collection
  // with an ID of 123ABC
}

match /users/{document} {
  // this will apply to all documents
  // in the users collection

  match /likes/{document} {
    // This will apply to all documents in    
    //the users/likes sub collection
  }

  match /hearts/{document=**} {
    // This will apply to all documents in    
    // the users/hearts sub collection as 
    // well as any sub collections that are 
    // further nested.

  }

}

match /users/{document=**} {
  // this will apply to all documents
  // in the users collection, as well as all 
  // documents in any sub collections under /users
}




Related