Created
September 2, 2021 08:36
-
-
Save vkiranmaniya/b16fb094c93fde9e60f9d17dc2e0d538 to your computer and use it in GitHub Desktop.
Common rules for firebase
This file contains hidden or 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
// No security, complete access allowed | |
{ | |
"rules": { | |
".read": true, | |
".write": true | |
} | |
} | |
// Fully secured | |
{ | |
"rules": { | |
".read": false, | |
".write": false | |
} | |
} | |
// Only authenticated users can read/write data | |
{ | |
"rules": { | |
".read": "auth != null", | |
".write": "auth != null" | |
} | |
} | |
// Checks auth uid equals database node uid | |
// In other words, the User can only access their own data | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".read": "$uid === auth.uid", | |
".write": "$uid === auth.uid" | |
} | |
} | |
} | |
} | |
// Validates user is moderator from different database location | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".write": "root.child('users').child('moderator').val() === true" | |
} | |
} | |
} | |
} | |
// Validates string datatype and length range | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".validate": "newData.isString() | |
&& newData.val().length > 0 | |
&& newData.val().length <= 140" | |
} | |
} | |
} | |
} | |
// Checks presense of child attributes | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".validate": "newData.hasChildren(['username', 'timestamp'])" | |
} | |
} | |
} | |
} | |
// Validates timestamp is not a future value | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
"timestamp": { | |
".validate": "newData.val() <= now" | |
} | |
} | |
} | |
} | |
} | |
// Prevents Delete or Update | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".write": "!data.exists()" | |
} | |
} | |
} | |
} | |
// Prevents only Delete | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".write": "newData.exists()" | |
} | |
} | |
} | |
} | |
// Prevents only Update | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".write": "!data.exists() || !newData.exists()" | |
} | |
} | |
} | |
} | |
// Prevents Create and Delete | |
{ | |
"rules": { | |
"posts": { | |
"$uid": { | |
".write": "data.exists() && newData.exists()" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment