Moody
Moody

Reputation: 164

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.

Upvotes: 1

Views: 161

Answers (2)

Moody
Moody

Reputation: 164

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.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599896

That first line looks off.

What do you expect this to do?

/databases/(default)/documents/users/$(request.auth.uid)

If you want to read a field from the document in Firestore, use firestore.get() there too, just as you do in the second line:

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

Upvotes: 1

Related Questions