Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
62 lines (54 loc) · 1.97 KB
/
Copy pathfirestore.rules
File metadata and controls
62 lines (54 loc) · 1.97 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function isAdmin() {
return isAuthenticated() &&
(get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin' ||
(request.auth.token.email == "amlunardev@gmail.com" && request.auth.token.email_verified == true));
}
function isPlayer(roomCode) {
let meta = get(/databases/$(database)/documents/rooms/$(roomCode)/meta/data).data;
return isAuthenticated() && (request.auth.uid == meta.p1 || request.auth.uid == meta.p2);
}
// Room rules
match /rooms/{roomCode} {
match /meta/data {
allow read: if isAuthenticated();
// Allow create if p1 is the current user
allow create: if isAuthenticated() && request.resource.data.p1 == request.auth.uid;
// Allow update if user is p1 or p2 (joining)
allow update: if isAuthenticated() && (
request.auth.uid == resource.data.p1 ||
(resource.data.p2 == null && request.resource.data.p2 == request.auth.uid) ||
request.auth.uid == resource.data.p2
);
}
match /pings/data {
allow read: if isPlayer(roomCode);
allow write: if isPlayer(roomCode);
}
match /games/{gameId} {
allow read, write: if isPlayer(roomCode);
match /{allSubcollections=**} {
allow read, write: if isPlayer(roomCode);
}
}
match /messages/{messageId} {
allow read: if isPlayer(roomCode);
allow create: if isPlayer(roomCode) && request.resource.data.sender in ['p1', 'p2'];
}
}
// User rules
match /users/{userId} {
allow read: if isAuthenticated();
allow write: if isOwner(userId) || isAdmin();
}
}
}