Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a function in firebase realtime rules

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

like image 864
Mahmoud Abu Alheja Avatar asked Oct 26 '25 06:10

Mahmoud Abu Alheja


2 Answers

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.

like image 90
Frank van Puffelen Avatar answered Oct 29 '25 10:10

Frank van Puffelen


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.

like image 35
Doug Stevenson Avatar answered Oct 29 '25 08:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!