Skip to content

Instantly share code, notes, and snippets.

@ThadeuLuz
Last active September 26, 2017 20:37
Show Gist options
  • Save ThadeuLuz/f742f9d9c48f661d37e8b71c34c388e6 to your computer and use it in GitHub Desktop.
Save ThadeuLuz/f742f9d9c48f661d37e8b71c34c388e6 to your computer and use it in GitHub Desktop.
Firebase Rules Example (Bolt + Targaryen)
{
"name": "devfest-2017-presentation",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "firebase-bolt rules.bolt --output rules.json",
"bolt": "nodemon --watch rules.bolt --exec \"npm run build\"",
"pretest": "npm run build",
"test": "targaryen --verbose rules.json tests.json",
"targaryen": "nodemon --watch tests.json --exec \"npm run test\""
},
"author": "",
"license": "ISC",
"dependencies": {
"firebase-bolt": "^0.8.2",
"nodemon": "^1.12.1",
"targaryen": "^3.0.2"
}
}
imLoggedIn() {
auth != null
}
myUidIs(uid) {
imLoggedIn() && auth.uid == uid
}
imServer() {
myUidIs('SERVER')
}
type User {
displayName: String,
age: Number
}
path /users { read() { imServer() } }
path /users/{userId} is User {
read() { myUidIs(userId) }
write() { myUidIs(userId) || imServer() }
}
{
"rules": {
"users": {
".read": "auth != null && auth.uid == 'SERVER'",
"$userId": {
".validate": "newData.hasChildren(['displayName', 'age'])",
"displayName": {
".validate": "newData.isString()"
},
"age": {
".validate": "newData.isNumber()"
},
"$other": {
".validate": "false"
},
".read": "auth != null && auth.uid == $userId",
".write": "auth != null && auth.uid == $userId || auth != null && auth.uid == 'SERVER'"
}
}
}
}

#Simple Firebase Rules Stack

{
"root": {
"users": {
"RICK": {
"displayName": "Rick Sanchez",
"age": 60
},
"MORTY": {
"displayName": "Morty Smith",
"age": 14
}
}
},
"users": {
"Rick": { "uid": "RICK" },
"Morty": { "uid": "MORTY" },
"Server": { "uid": "SERVER" }
},
"tests": {
"users": {
"canRead": ["Server"],
"cannotRead": ["Rick", "Morty"]
},
"users/MORTY": {
"canRead": ["Server", "Morty"],
"cannotRead": ["Rick"]
},
"users/RICK/age": {
"canWrite": [{ "auth": "Rick", "data": 59 }]
},
"users/RICK/displayName": {
"canWrite": [{ "auth": "Rick", "data": "Pickle Rick" }],
"cannotWrite": [
{ "auth": "Rick", "data": null },
{ "auth": "Rick", "data": 123 },
{ "auth": "Rick", "data": { "no": "object" } }
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment