Last active
March 30, 2020 20:57
-
-
Save zeroows/3ae65bcf10bd6353d3b55140d89a4873 to your computer and use it in GitHub Desktop.
Firestore Rules Example in Google Console
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
rules_version = '2'; | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match /shoppinglist/{lists} { | |
function isSignedIn() { | |
return request.auth != null; | |
} | |
function isEmailVerified() { | |
return request.auth.token.email_verified != false; | |
} | |
function isKnownUser() { | |
return isSignedIn() && request.auth.uid != "" && isEmailVerified(); | |
} | |
function UserId() { | |
return request.auth.uid; | |
} | |
function getOwner(rsc) { | |
// Read Owner in the resource (rsc). | |
return rsc.data.owner; | |
} | |
function getShares(rsc) { | |
// Read Shares in the resource (rsc). | |
return rsc.data.shares; | |
} | |
function isOwner(rsc) { | |
// Determine if the user is the owner | |
return isKnownUser() && (getOwner(rsc) == UserId()); | |
} | |
function isShared(rsc) { | |
// Determine if the user was shared the list | |
return isKnownUser() && (UserId() in getShares(rsc)); | |
} | |
function getParentDoc(){ | |
// Get the parent document | |
return get(/databases/$(database)/documents/shoppinglist/$(lists)); | |
} | |
allow read: if isOwner(resource) || isShared(resource); | |
allow write: if isOwner(resource); | |
match /items/{items} { | |
allow read: if isOwner(getParentDoc()) || isShared(getParentDoc()); | |
allow write: if isOwner(getParentDoc()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment