Skip to content

Instantly share code, notes, and snippets.

@DennisAlund
Last active April 19, 2024 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisAlund/47f48e2d07f595c082327abe2f254792 to your computer and use it in GitHub Desktop.
Save DennisAlund/47f48e2d07f595c082327abe2f254792 to your computer and use it in GitHub Desktop.
Firestore rules for article
service cloud.firestore {
match /databases/{database}/documents {
// Alt A: Using roles stored in Firestore user documents to determine access
match /collection-a/{document} {
allow read: if 'admin' in getUserRoles();
}
// Alt B: Using auth claims (role as an array) to determine access
match /collection-b/{document} {
allow read: if request.auth != null && 'admin' in request.auth.token.roles;
}
// Function to get user roles from Firestore document
function getUserRoles() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles;
}
}
}
service firebase.storage {
match /b/{bucket}/o {
// Alt A: Using roles in user documents to determine access
match /folder-a/{allPaths=**} {
allow read: if 'admin' in getUserRoles();
}
// Alt B: Using auth claims to determine access
match /folder-b/{allPaths=**} {
allow read: if request.auth != null && 'admin' in request.auth.token.roles;
}
// Function to get user role from Firestore document
function getRoleFromFirestore() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment