-
-
Save xprilion/95a87669188f211343fafe1d0b2bdabc to your computer and use it in GitHub Desktop.
Firestore Rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Basic public access | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match /{document=**} { | |
allow read, write: if true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Authenticated Access | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match /{document=**} { | |
allow read, write: if request.auth != null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Role-Based Access | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match /posts/{postId} { | |
allow read: if true; | |
allow write: if request.auth.token.admin == true; | |
} | |
match /users/{userId} { | |
allow read, write: if request.auth.uid == userId; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data Validation and Conditional Access | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match /posts/{postId} { | |
allow read: if true; | |
allow write: if request.auth.token.admin == true && request.resource.data.keys().hasAll(['title', 'content', 'timestamp']); | |
} | |
match /messages/{messageId} { | |
allow read, write: if request.auth.uid != null && request.resource.data.authorId == request.auth.uid | |
&& request.resource.data.timestamp > request.time; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Complex Rules with Function | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
function isOwner(userId) { | |
return request.auth.uid == userId; | |
} | |
function isValidPost(post) { | |
return post.keys().hasAll(['title', 'content', 'timestamp']) && post.timestamp > request.time; | |
} | |
match /posts/{postId} { | |
allow read: if true; | |
allow write: if request.auth.token.admin == true && isValidPost(request.resource.data); | |
} | |
match /users/{userId} { | |
allow read, write: if isOwner(userId); | |
} | |
match /messages/{messageId} { | |
allow read: if isOwner(request.resource.data.authorId); | |
allow write: if isOwner(request.resource.data.authorId) && request.resource.data.timestamp > request.time; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment