Skip to content

Instantly share code, notes, and snippets.

@ThadeuLuz
Last active September 26, 2017 20:37

Revisions

  1. ThadeuLuz revised this gist Sep 26, 2017. 2 changed files with 1 addition and 2 deletions.
    2 changes: 0 additions & 2 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    node_modules
    rules.json
    1 change: 1 addition & 0 deletions Simple Firebase Rules Stack.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #Simple Firebase Rules Stack
  2. ThadeuLuz created this gist Sep 26, 2017.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    node_modules
    rules.json
    19 changes: 19 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    {
    "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"
    }
    }
    23 changes: 23 additions & 0 deletions rules.bolt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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() }
    }
    21 changes: 21 additions & 0 deletions rules.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    {
    "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'"
    }
    }
    }
    }
    40 changes: 40 additions & 0 deletions tests.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    {
    "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" } }
    ]
    }
    }
    }