• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Implementing Submission Voting Logic In Firebase

Front End Development · November 18, 2016

I love Firebase. The power and flexibility of using it alongside my favourite client-side framework Aurelia, unrivalled. Recently whilst building the Built With Aurelia website which uses Firebase, I wanted the ability for logged in users to vote on particular submissions.

My Firebase structure is literally the following:

submissions: {
    pokegorun: {
        added: 1470919840796,
        category: "website"
        description: "Pokego.run is a Pokemon spotting map where you ..."
        name: "Pokego.run"
        repoUrl: "https://github.com/Vheissu/pokego.run-public"
        url: "https://pokego.run",
        votes: {
            TPdM9feOrbgNHVGHebBT7TBZ8Xj1: true
        }
    }
}

My submissions object contains submissions keyed by their name. Then inside some basic object properties for this submission and an object for votes which are keyed by the username (so the same user can’t vote twice).

Under the “Rules” tab in Firebase I have the following for my database:

{
    "rules": {
        ".read": true,
        "submissions": {
            "$slug": {
              ".write": "auth.uid != null",
              "votes": {
                "$uid": {
                    ".write": "auth.uid != null && auth.uid === $uid"
                }
              }
            }
        }
    }
}

As you can see I have a basic read rule to allow everything to be read by default. Then I define a rule group for my submissions “table” in which I only allow writes if the user is logged in (non-authenticated users can’t submit anything).

Then we get to the meat on the bones, the “votes” part sets up a sub-rule group. This is essentially saying that each item is keyed by a user ID which we are calling “$uid” and then inside we are saying that only the user who has previously voted can alter the vote (retract it and or resubmit their vote). Any registered user can submit a vote because of our .write rule.

Once you harness the power of Firebase’s rules, you can create anything.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Mock uuid In Jest
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz