Today, I have a look at Firebase security rules for the purpose of implementing user roles. I share a quick example that includes both a database and corresponding rules to help you get up to speed with the format.
There’s no convenient interface in Firebase where roles can be readily added. The semantics are instead defined both through the JSON database along with the logic in the security rules.
Let’s say I have a
secret_data path that I want to only be accessed only if a user is a
My database looks like this.
The choice of where I place the roles is entirely up to me and how I want to work with my data. The only structure that is imposed on me is the JSON hierarchy.
It helps to restrict all access by default. That is easily accomplished by making the top level read and write in the rules be both false (lines 2–4 below).
Getting access to the
secret_data, a path in my JSON database, involves setting up a boolean evaluation of the existence of my uid within the
I look at the tree, starting at roles and descend into
paid_user and check for the existence of my uid within that path (line 8 below).
I’ve also added an additional check to verify that my uid also appears within the users that are authorized to access the secret data (line 9 below).
In other words, the implementation of my security model can be as complex as I want it to be.
There is an additional check of matching my uid to the logged-in user’s uid to be able to access any user information there (line 12 below).
In this case, that additional check is completely redundant because the preceding logic will override anything below.
That is why the top level read, write rules must be set to false.
Here are the security rules as I’ve described them.
In conclusion, Firebase allows creating just about any security model you want. The key to creating the rules is to look at your data and match the structure within your rules and then add the appropriate logic. Having a couple consoles open side-by-side facilitates this process.
That’s it for my quick and easy introduction of how to approach making user roles in Firebase.