I am using Firebase database to store student/teacher database. Right now my rules allow teachers to read their student's data by calling a callable function which provide the data related with the student. (Firebase admin sdk)
What I am trying to do -
- Allow teachers to invite students to add them under his student list. Teachers can send an invite by calling a http firebase function to send invitation via email.
- Once students clicks on the invite link to accept, they get added under the teacher who sent them invitation.
- After acceptance, teacher can read the data of all the student registered under him by calling a callable function. This is where I have problem. I can read properly with no problem but calling a function everytime page refresh, are gonna be expensive hence I decided to play with the firebase rules.
Below is my structure of firebase database. How can I design the database rule such a way that a teacher can read their student data without calling the callable function.
I have found something similar on this stackoverflow post -
DATABASE
{
"teachers":
{
"teacher-unique-id":
{
"invited":
{
"auto-gen-key-id": "student-email-id-1",
"auto-gen-key-id": "student-email-id-2",
"auto-gen-key-id": "student-email-id-3",
"auto-gen-key-id": "student-email-id-4",
"auto-gen-key-id": "student-email-id-5",
},
"accepted":
{
"auto-gen-key-id": "unique-student-id-1",
"auto-gen-key-id": "unique-student-id-2",
"auto-gen-key-id": "unique-student-id-3",
}
}
},
"students":
{
"unique-student-id-1":
{
"session-id-1":
{
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
},
"session-id-2":
{
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
}
},
"unique-student-id-2":
{
"session-id-1":
{
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
},
"session-id-2":
{
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
"auto-gen-key-id": "data",
}
}
}
}
Firebase rules - This is basically default one I am using. How can I modify them to do what I want to achieve?
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
"teachers":{
"$uid":{
"studentsId":{
".read": "$uid===auth.uid",
}
}
},
"students":{
"$uid":{
".read": "$uid===auth.uid",
".write": "$uid===auth.uid"
}
}
}
}