'Firebase RealtimeDatabase rules

I want to use the Firebase Realtimedatabase in my flutter project. I have added everything and it works :)

But now I want to filter the data, so that not every user listens to all files.

My Database structure looks like this:

party-5f337
party
    -M78sk5rfIzJZAL2SxLk
        amount: 700
        created: "2020-05-12T19:16:11.577318"
        currentAmount: 4142
        name: "Test"
        userids

Now I want to show every user every party he is in the list of userids. I can filter the Data in my Flutter project but I think it's not nice to load all data and than filter.

Is it possible with the rules from firestore to only get the parties the user is in?

My rules so far

  /* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
    "rules": {
        ".write": "auth != null",   // all user can write

"party": {
   ".read": "data.child('party').child('userids').contains(the user id)"   //just pseudocode

   }
 }
}

But it doesn't work. I tested different rules but I can't solve my problem.

Can I use the realtimedatabase for this? How I have to write the rule?

Thanks for help :)

Regards Simon



Solution 1:[1]

You can only use 'data.' under a variable.

"$party":{
".read":"data.child('userids').contains(auth.uid)"
}

By adding $ before the word party you make a variable of party. The command 'data.' points to that variable, and now the code should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omer