How to set Firebase database rules for a group messaging app when using Phone Authentication

45 views
Skip to first unread message

Raza Rehman

unread,
Jul 7, 2019, 7:27:25 PM7/7/19
to Firebase Google Group
Hello

I am working on a group messaging app.When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created and below that is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.


{
 
"rules": {
   
"plan":{
     
"$planID":{
       
"messages":{
         
".read":"data.parent().child('members').child(auth.uid).exists()",
         
".write":"data.parent().child('members').child(auth.uid).exists()"
       
}
     
}
   
}
 
}
}

HjnNC.png


The problem is that after setting the above database rules even the authenticated users cannot create new group or post a new message. Please any help or suggestion would be much appreciated. 

Thanks

Kato Richardson

unread,
Jul 8, 2019, 11:22:10 AM7/8/19
to Firebase Google Group
Hello Raza,

That rule should work as long as the members/$uid record exists. So perhaps the first time you're creating it, you haven't written that yet (or you have a race condition and didn't wait for the write to succeed).

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/6b0e532c-7069-480e-a5af-9f83a65c592b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Raza Rehman

unread,
Jul 8, 2019, 6:09:42 PM7/8/19
to fireba...@googlegroups.com
Hello Kato, 

Thank you very much for your reply. I am sure there is no race condition. The members node is being populated correctly with $uid from the users node at the bottom of the image. But still I am unable to read and write once I set the rules that I have posted.

Thanks
  Raza

HUSSAIN ALIj

unread,
Jul 8, 2019, 6:10:17 PM7/8/19
to Firebase Google Group
بتاريخ الاثنين، 8 يوليو، 2019 2:27:25 ص UTC+3، كتب Raza Rehman:
> Hello
>
>
>
> I am working on a group messaging app.When creating the app I was using default rules for the Firebase database. But now I am trying set rules so that all the uid available in the members node can see or send messages to that particular group. Below are the latest set of rules that I have created and below that is my detailed database structure after a plan is created. Please note that I am making use of Firebase Phone Authentication. For this reason I am creating a users node which keeps track of uid against every registered number. When a user selects members to be added to the group I check those numbers against their respective uid's and then add those uid's in the member's node.
>
>
>
>
> {
>   "rules": {
>     "plan":{
>       "$planID":{
>         "messages":{
>           ".read":"data.parent().child('members').child(auth.uid).exists()",
>           ".write":"data.parent().child('members').child(auth.uid).exists()"
>         }
>       }
>     }
>   }
> }
>
>
>
>
>
>

Raza Rehman

unread,
Jul 12, 2019, 6:04:28 PM7/12/19
to Firebase Google Group
Hello 

I also posted my problem on Stack overflow and got the following answer. I have gone through documentation for Firebase  database rules but I cannot seem to get the rules to work. Is there a way to set rules for when sub-node are integers? Is there a way to set rules for array of items in Firebase?

Please any help and suggestions would be very helpful.

Thanks
Reply all
Reply to author
Forward
0 new messages