-
Notifications
You must be signed in to change notification settings - Fork 14
/
firestore.rules
39 lines (31 loc) · 1.12 KB
/
firestore.rules
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow update: if request.auth.uid == userId;
allow create: if request.auth != null;
allow read;
}
match /users/{userId}/{document=**} {
allow read, write: if request.auth.uid == userId;
}
function isValidUsername(username) {
let isOwner = request.auth.uid == request.resource.data.uid;
let isValidLength = username.size() >= 3 && username.size() <= 15;
let isValidRegex = username.matches("^[a-z][a-z0-9]*([._-][a-z0-9]+)*$");
let isValidUserDoc = exists(/databases/$(database)/documents/users/$(request.auth.uid));
return isOwner && isValidLength && isValidRegex && isValidUserDoc;
}
match /usernames/{username} {
allow read;
allow create: if isValidUsername(username);
}
match /users/{userId}/notes/{noteId} {
allow write: if request.auth.uid == userId;
allow read: if isPublicNote() || request.auth.uid == userId;
}
function isPublicNote() {
return resource.data.public == true;
}
}
}