Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow writes from service account in Firebase?

I would like make writes to my Firebase data from a node.js server but deny all writes to any other client. Any other client should only be allowed read access. What is the best approach for this? Would I need to authenticate from node.js server with a special admin account and setup a security rule for that specific account or is there some better way to accomplish this? Thanks!

like image 492
nomad Avatar asked May 25 '16 00:05

nomad


People also ask

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

What are the restrictions in Firebase?

Firebase Security Rules work by matching a pattern against database paths, and then applying custom conditions to allow access to data at those paths. All Rules across Firebase products have a path-matching component and a conditional statement allowing read or write access.


2 Answers

Just use firebase-admin on your server with a service account. It will have Administrative Privileges by default.

And for your rules, all you need is this:

{
  "rules": {
    ".read": true,
  }
}

or this if you only want logged in users to read

{
  "rules": {
    ".read": "auth !== null",
  }
}
like image 197
ThadeuLuz Avatar answered Oct 18 '22 08:10

ThadeuLuz


In your node server use the Admin SDK to log in with service account

and in the database rules use this:

{
  "rules": {
    ".write": "false"
    ".read": "true",
  }
}
like image 27
Shaytj Avatar answered Oct 18 '22 09:10

Shaytj