-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirebase.rules
More file actions
66 lines (58 loc) · 2.01 KB
/
firebase.rules
File metadata and controls
66 lines (58 loc) · 2.01 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
66
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isAdmin() {
return isAuthenticated() &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function hasActivePremium(userId) {
return get(/databases/$(database)/documents/users/$(userId)).data.subscription.tier == 'premium' &&
get(/databases/$(database)/documents/users/$(userId)).data.subscription.validUntil > request.time;
}
// Users collection
match /users/{userId} {
allow read: if isAuthenticated() && (isOwner(userId) || isAdmin());
allow create: if isAuthenticated() && isOwner(userId);
allow update: if isAuthenticated() && (isOwner(userId) || isAdmin());
allow delete: if isAdmin();
}
// Tutorials collection
match /tutorials/{tutorialId} {
allow read: if
!resource.data.isPremium ||
(isAuthenticated() && hasActivePremium(request.auth.uid));
allow write: if isAdmin();
}
// Progress collection
match /progress/{progressId} {
allow read: if isAuthenticated() && (
resource.data.userId == request.auth.uid || isAdmin()
);
allow create, update: if isAuthenticated() && (
request.resource.data.userId == request.auth.uid
);
allow delete: if isAdmin();
}
// Subscriptions collection
match /subscriptions/{subscriptionId} {
allow read: if isAuthenticated() && (
resource.data.userId == request.auth.uid || isAdmin()
);
allow write: if isAdmin();
}
// Checkout Sessions collection
match /checkoutSessions/{sessionId} {
allow read: if isAuthenticated() && (
resource.data.userId == request.auth.uid || isAdmin()
);
allow write: if isAdmin();
}
}
}