Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I give write permission to a specified user in Firebase?

I have a mobile application which reads the data from the firebase server without firebase login/authentication (posts and news) and I want to create an admin webpage where I can log in and add, or modify news, so I need a write permission there. My rules are currently:

{
  "rules": {
    ".read": true,
    ".write": "auth !== null && ?????
  }
}

Can I write something like "user.emailAddress == '[email protected]'"?

like image 694
Ambrus Tóth Avatar asked Oct 14 '25 17:10

Ambrus Tóth


2 Answers

You can create a users table on database like

{
  "users":{
     "your UID":{
        "isAdmin": true
      }
   }
}

Then edit rules :

{
  "rules": {
    ".read": true,
    ".write": "auth.uid != null && root.child("users").child(auth.uid).isAdmin === true"
  }
}
like image 75
Cemsina Güzel Avatar answered Oct 17 '25 12:10

Cemsina Güzel


You might want to start by reading the documentation about securing user data. There is a lot to know here.

One possibility is using the known user's uid to restrict access. The auth.uid variable contains the uid.

".write": "auth.uid == 'the-known-uid'"

Also you can use auth.token to access some other things about the user, including email address (which may not be present):

".write": "auth.token.email == '[email protected]'"

You can also use custom authentication tokens, which also is covered in the documentation.

like image 33
Doug Stevenson Avatar answered Oct 17 '25 10:10

Doug Stevenson