-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
58 lines (52 loc) · 2.65 KB
/
firestore.rules
File metadata and controls
58 lines (52 loc) · 2.65 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
47
48
49
50
51
52
53
54
55
56
57
58
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;
}
// Usernames lookup collection (enforces uniqueness)
match /usernames/{username} {
allow read: if isAuthenticated();
allow create: if isAuthenticated() && request.resource.data.uid == request.auth.uid;
allow delete: if isAuthenticated() && resource.data.uid == request.auth.uid;
allow update: if false; // usernames cannot be reassigned
}
// Users collection
match /users/{userId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
// Projects collection
match /projects/{projectId} {
allow read: if isAuthenticated() && (request.auth.uid == resource.data.userId || resource.data.isPublic == true);
allow create: if isAuthenticated() && request.resource.data.userId == request.auth.uid;
// Only allow updating safe fields — storageURL, contentHash, userId are immutable after creation
allow update: if isAuthenticated()
&& request.auth.uid == resource.data.userId
&& request.resource.data.userId == resource.data.userId
&& request.resource.data.contentHash == resource.data.contentHash
&& request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['title', 'isPublic', 'tags', 'updatedAt', 'thumbnailData']);
allow delete: if isAuthenticated() && request.auth.uid == resource.data.userId;
}
// Gallery collection — sharing to gallery is an explicit user action that
// opts the item into public visibility. All gallery items are readable by
// any authenticated user by design (no per-item visibility flag needed).
match /gallery/{itemId} {
allow read: if isAuthenticated();
allow write: if isAuthenticated() && request.auth.uid == resource.data.userId;
allow create: if isAuthenticated() && request.resource.data.userId == request.auth.uid;
}
// NFTs collection (listed NFTs are readable by any authenticated user)
match /nfts/{nftId} {
allow read: if isAuthenticated() && (request.auth.uid == resource.data.userId || resource.data.isListed == true);
allow write: if isAuthenticated() && request.auth.uid == resource.data.userId;
allow create: if isAuthenticated() && request.resource.data.userId == request.auth.uid;
}
}
}