I'm building a real-time messenger using Ionic/Angular and Firebase. I'm using Firebase Authentication to authenticate my users, and Firebase DB to store the messages and message threads.
My problem what security rules should I use in order for message threads to be private and viable and writable only to the 2 persons engaged in a given message thread.
My DB structure looks like this now:
-chats
--Message_thread_ID
---Unique_ID_generated_message by Firebase
----Message
----Sender ID
----Receiver ID
---Unique_ID_generated_message by Firebase
----Message
----Sender ID
----Receiver ID
--Message_thread_ID
---Unique_ID_generated_message by Firebase
----Message
----Sender ID
----Receiver ID
My current firebase security rules are:
{
"rules": {
".read": "auth != null",
".write": false,
"$messageThreadID": {
".write": "auth != null && !data.exists()",
".read": "auth != null"
}
}
}
This works fine, but this way any authenticated user will be able to read all messages between any user. Which is no what I want. I want to keep message threads private to only the two users in any given thread.
How should I restructure my DB for the best optimal solution?
PS: I have ways to do it using backend, or making two copies for each of the two users of their respective threads, but these solutions look really subprime.