Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore rules compare reference to constructed path

In my company documents, I have a reference field named owner, which points to a user document. In the rule, I am trying to check if the authenticated uid is the owner of the company:

match /companies/{companyId} {
  allow read: if isOwner(resource.data.owner, request.auth.uid);
}



function isOwner(owner, userId) {
   return path('/users/' + userId) == owner;
}

I tried many things but can't figure out how to make this work.

(I know using a string instead of a reference works, but I would rather use a reference)

like image 210
Yan Avatar asked Sep 03 '26 03:09

Yan


1 Answers

When you construct the path, include this prefix: /databases/(default)/documents/. It's part of the full path to a document.

match /companies/{companyId} {
  allow read: if isOwner(resource.data.owner, request.auth.uid);
}

function isOwner(owner, userId) {
   return path('/databases/(default)/documents/users/' + userId) == owner;
}
like image 156
Juan Lara Avatar answered Sep 04 '26 21:09

Juan Lara