Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
46 lines (40 loc) · 1.43 KB
/
Copy pathfirestore.rules
File metadata and controls
46 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper function to check if user is authenticated
function isAuthenticated() {
return request.auth != null;
}
// Helper function to check if user owns the document
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
// Allow access to all listings via collection group query
match /{path=**}/listings/{listingId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated();
allow update: if isOwner(resource.data.userId);
allow delete: if isOwner(resource.data.userId);
}
// Listing interests rules
match /listingInterests/{listingId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated();
allow update: if isAuthenticated() &&
// Only allow updating if not changing other users' data
(!request.resource.data.diff(resource.data).affectedKeys()
.hasAny(['listingId']) || resource.data.listingId == request.resource.data.listingId);
allow delete: if false;
}
// User profiles
match /profiles/{userId} {
allow read: if isAuthenticated();
allow write: if isOwner(userId);
}
// Starred listings
match /starredListings/{userId}/listings/{listingId} {
allow read: if isAuthenticated();
allow write: if isOwner(userId);
}
}
}