-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirestore.rules
More file actions
53 lines (46 loc) · 1.52 KB
/
firestore.rules
File metadata and controls
53 lines (46 loc) · 1.52 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
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 resource
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
// Helper function to check if the document belongs to the user
function belongsToUser() {
return isAuthenticated() && request.auth.uid == resource.data.userId;
}
// Helper function to check if the request is from Firebase Admin SDK (backend)
function isBackend() {
return request.auth.token.admin == true;
}
// Rules for users collection
// Frontend: Read only for own profile
// Backend: Full access
match /users/{userId} {
allow read: if isOwner(userId);
allow write: if isBackend();
}
// Rules for customers collection
// Frontend: Read only for own customers
// Backend: Full access for CRUD operations
match /customers/{customerId} {
allow read: if belongsToUser();
allow write: if isBackend();
}
// Rules for invoices collection
// Frontend: Read only for own invoices
// Backend: Full access for CRUD operations
match /invoices/{invoiceId} {
allow read: if belongsToUser();
allow write: if isBackend();
}
// Deny all other access
match /{document=**} {
allow read, write: if false;
}
}
}