Hello guys Avijit Das here, And today I am showing
how you can secure your Firebase Real Time Database from hackers.
So let’s start
ㅤ
Rule No 1 :
Basic rules - Everyone can read and write your full database.
{
"rules": {
".read": "true",
".write": "true"
}
}
ㅤ
Rule No 2 :
Basic rules - No one will be able to read and write your database.
{
"rules": {
".read": "false",
".write": "false"
}
}
ㅤ
Rule No 3 :
Basic rules - Any authenticated user can read and write your full database.
{
"rules": {
".read": "auth.uid != null",
".write": "auth.uid != null"
}
}
ㅤ
Rule No 4 :
Strong rules - Only user and admin can read and write user/{user_id}/password_activity/
Tag.
Rules :
{
"rules": {
".read": "false",
".write": "false",
"user": {
"$uid": {
"password_activity":{
".read": "$uid === auth.uid
|| auth.token.email.endsWith(\"@admin.example.com\")",
".write": "$uid === auth.uid
|| auth.token.email.endsWith(\"@admin.example.com\")",
},
}
}
}
}
Note : admin account email must be end with @admin.example.com
Example : [email protected]
ㅤ
Rule No 5 :
Strong rules - Any authenticated user can read user/{user_id}/password_activity/
but only user can write his password activity.
Rules :
{
"rules": {
".read": "false",
".write": "false",
"user": {
"$uid": {
"password_activity":{
".read": "auth.uid != null",
".write": "$uid === auth.uid",
},
}
}
}
}
ㅤ
Rule No 6 :
For example user2 need to create a data in user/user1/friends/
tag.
Example :
Rules :
{
"rules": {
".read": "false",
".write": "false",
"user": {
"$uid": {
"friends" : {
"$friend_uid": {
".read": "auth.uid != null",
".write": "$friend_uid === auth.uid",
},
}
}
}
}
}
Note : Any authenticated user can read user/user1/friends/
tag but only user2
can create his data in user/user1/friends/
tag.
ㅤ
Note : I know this guide really hard to understand. but listen good things never come easily.
If this help you then just press ❤️ button