Search code examples
google-cloud-firestorefirebase-storagefirebase-security

How to use Firestore Document Reference in Firebase Cloud Storage Security Rules?


allow write: if /databases/(default)/documents/users/$(request.auth.uid) in 
  firestore.get(/databases/(default)/documents/businesses/$(businessDocId)).data.team;

The get().data.team value is an array which contains a Firestore document reference type.

I'm getting a permission error when using this for Cloud Storage, although a similar call in Firestore security rules works.


Solution

  • Working solution:

    function isBusinessOwner(businessDocId) {
      let businessTeamArray = firestore.get(/databases/(default)/documents/businesses/$(businessDocId)).data.team;
      let businessOwner = businessTeamArray[0];
      return businessOwner in businessTeamArray;
    }
    

    Unfortunately with this solution, I have to manually choose an index in the array, therefore restricting the possibility of having multiple business owners in the array to compare against.