-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
65 lines (55 loc) · 1.96 KB
/
Copy pathfirestore.rules
File metadata and controls
65 lines (55 loc) · 1.96 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
59
60
61
62
63
64
65
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;
}
// Users collection - users can only read/write their own data
match /users/{userId} {
allow read: if isOwner(userId);
allow create: if isAuthenticated() && request.auth.uid == userId;
allow update: if isOwner(userId);
allow delete: if isOwner(userId);
// Nutrition history subcollection
match /nutrition_history/{entryId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
// Favorites subcollection
match /favorites/{foodId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
// Meal logs subcollection
match /meal_logs/{logId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
// Nutrition AI chat/conversation subcollection
match /conversations/{entryId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
}
// Myths collection - public read, authenticated write
match /myths/{mythId} {
allow read: if true;
allow create: if isAuthenticated();
allow update: if isAuthenticated();
allow delete: if isAuthenticated() && (
request.auth.uid == resource.data.askedBy ||
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin'
);
}
// Admin-only collections
match /admin/{document=**} {
allow read, write: if isAuthenticated() &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
}
}