Skip to content

Instantly share code, notes, and snippets.

@xprilion
Last active January 20, 2024 07:30
Show Gist options
  • Save xprilion/95a87669188f211343fafe1d0b2bdabc to your computer and use it in GitHub Desktop.
Save xprilion/95a87669188f211343fafe1d0b2bdabc to your computer and use it in GitHub Desktop.
Firestore Rules
// Basic public access
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
// Authenticated Access
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
// 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;
}
}
}
// 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;
}
}
}
// 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