Reputation: 2470
In Firestore I have 2 collections
A users collection and a periods collection. I need a rule for my period collection : Read and write only by the user who create the period.
This is what I try with no success
service cloud.firestore {
match /databases/{database}/documents {
function userDoc() {
return /databases/$(database)/documents/users/$(request.auth.uid);
}
match /users/{userId} {
allow read: if true;
allow create, update: if request.auth.uid == userId;
}
match /periods/{id} {
allow read : if userDoc() == request.resource.data.user;
allow write : if userDoc() == request.resource.data.user;
}
}
}
Upvotes: 2
Views: 735
Reputation: 1586
For your use case please see the below firestore rule sample.
service cloud.firestore {
match /databases/{database}/documents {
function userDoc() {
return /databases/$(database)/documents/users/$(request.auth.uid);
}
match /users/{userId} {
allow read: if true;
allow create, update: if request.auth.uid == userId;
}
match /periods/{id} {
allow read, delete : if userDoc() == resource.data.user_id;
allow update : if userDoc() == request.resource.data.user_id;
allow create : if userDoc() == /databases/$(database)/documents/$(request.resource.data.user_id.path);
}
}
}
Upvotes: 1
Reputation: 41
Replace
if userDoc() == request.resource.data.user
with
if userDoc() == request.resource.data.user_id
Other than that your security rule looks correct to me.
Upvotes: 1