I'm writing a custom cloud function (official documentation) in Firestore security rules that checks to make sure the submitting user is the content owner, per the official documentation.
I'm applying the custom function to the correct path /collection/{documents}/subcollection/{subcollectiondocuments}
where every subcollectiondocument
has a userId
field that's not null. These documents do not have a uid
field, but I tried it anyway in 2 and 4 below.
All versions of the custom function below (belongsToRequestor 1,2,3 and 4) generate a "Property resource is undefined on object" error in the Cloud Firestore rules playground simulator.
Do I need to pass something into the custom function, or am I making some other mistake?
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function belongsToRequestor1() {
return
request.auth.uid == resource.data.userId;
}
function belongsToRequestor2() {
return
request.auth.uid == resource.data.uid;
}
function belongsToRequestor3() {
return
request.auth.uid == request.resource.data.userId;
}
function belongsToRequestor4() {
return
request.auth.uid == request.resource.data.uid;
}
…
match /collection/{documents}/subcollection/{subcollectiondocuments} {
allow update: if
belongsToRequestor1();
// or belongsToRequestor2(); or belongsToRequestor3(); or belongsToRequestor4();
…
} }
I'm not sure how to implement this answer to another question to "…enter the path to an actual document that exists if you want to test your rule that uses its field values." Each document in subcollectiondocuments
has an id auto-generated by firebase.
Update: adding database screenshot (with fake data), as requested (properties = collection and reviews == subcollection):
Thanks for any help!