Firestore Security Rules Recipes — GoTut: Game and Other Tutorials

service cloud.firestore { match /databases/{database}/documents { // here will be your rules // that grant access to specific documents of collections } }

Write and Read

service cloud.firestore { 
match /databases/{database}/documents {
match /{document=**} {
allow write: if false;
allow read: if true;
}
}
}
get // allows to get a specific document 
list // allows to read or query a collection
create // allows to create a document 
delete // allows to delete a document
update // allows to update a document

Basics of Firestore Security Rules

resource.data
request.auth
resource.data.name is string
match /{document=**} // the rules apply to all sub-collections match /product/xyz // the rules apply to this specific path match /product/{productId} // the rules apply to this yet-to-be path

Useful Firestore Security Rules

Allow Access to Signed-In Users

match /profile/{profileID} { 
allow read: if request.auth != null;
}

Allow the Owner of a Document Access

match /accounts/{userID} { 
allow read, write: if isOwnerOf(userID);
}
function isOwnerOf(userID) {
return request.auth.uid == userID
}
match /accounts/{userID} { 
allow read, write: if isOwnerOf(userID);
}
function isOwnerOf(userID) {
return request.auth.uid == resource.data.uid;
}

Allow Access to Specific Documents

match /collection/{docID} { 
allow read: if resource.data.status == 'open';
}
match /collection/{docID} { 
allow read: if resource.data.isOpen;
}
match /collection/{docID} { 
allow read: if get(/databases/$(database)/documents/otherCollection/$(docID)).data.isOpen;
}
match /collection/{docID} { 
allow read: if exists(/databases/$(database)/documents/collection/$(docID));
}

More Information

--

--

In my spare time I work on Timeless Adventure — a fantasy adventure game

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store