Declaring a function in firebase realtime rules

Viewed 176

I use firebase realtime as database for my app and off course i used rules to secure my data so more node database then more rules and now have a big code inside it

my question is about how to define function to make my rules more readable and make it less code

I would to get like this

"rules": {

           function isMerchant(auth){
             return "root.child('Merchant').child(auth.token.phone_number).exists()";
           }

           "UsersMetaData" :{
                    "HistoryMarketVisit":{
                      "$visitId":{
                        ".read" :  isMerchant(auth),
                        ".write" :  isMerchant(auth),
                      }
                    },

              },
    },

i see this answer for same question but is for firestore , but want to achieve it in firebase real time

thanks for help

2 Answers

Unfortunately, Realtime Database rules don't have support for custom functions like you see in Firestore. You will have to duplicate the logic wherever it's needed.

As Doug also answered, the Firebase Realtime Database security rules don't natively have support for declared functions in there.

What you can do however is use Firebase's Bolt security modeling language and compiler. Bolt does support callable functions, by expanding them in the way you'd otherwise do manually, and has many other nifty features.

Related