0

I want to authenticate my node.js app to write data to the Firebase Realtime Database and allow clients to read. The structure of my DB schema right now is:

"<db_name>": {
  "users": { ... }
}

This is the rules config I've got so far:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": true
      },
      ".write": true
    }
  }
}

Shouldn't this at least allow all write access to the DB? And also, I tried searching for official documentation of how to authenticate the server app with the Firebase DB but what I can find is only how to use the Firebase Admin (which is not what I need).

In my code I do:

const firebaseConfig = {
  "apiKey": "xxx",
  "authDomain": "xxx.firebaseapp.com",
  "databaseURL": "https://xxx.firebaseio.com",
  "projectId": "xxx",
  "storageBucket": "xxx.appspot.com",
  "messagingSenderId": "xxx"
}
firebase.initializeApp(firebaseConfig);

But this is just to initialize the connection. I also need to authorize my server with Firebase somehow, through long-lasting token or smth, I am not sure...

Here's the only sensible thing I found, but it's from 2015 and the API has changed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
  • Client-side node is indeed not very well documented in the guides. For the full reference documentation for that platform, see https://firebase.google.com/docs/reference/node/. There is a [`signInWithEmailAndPassword` method](https://firebase.google.com/docs/reference/node/firebase.auth.Auth#signInWithEmailAndPassword), which should allow you to sign in as a regular user. – Frank van Puffelen Dec 09 '18 at 13:50
  • Hi Milkncookiez, did you ever find a solution to this?? I would also like to authenticate my server so that my server is authorised to read/write once I set rules such as `auth !== null` – daniel blythe May 09 '19 at 07:05

1 Answers1

1

Most of the documentation is focused on using Node.js in a trusted environment, where you use the Admin SDK to gain full administrative access. If you're on a trusted platform, I recommend using that, and then call setDatabaseAuthVariableOverride to access the database with limited privileges.

For the reference documentation for client-side node.js in non-trusted environments, see https://firebase.google.com/docs/reference/node/. There is a signInWithEmailAndPassword method, which should allow you to sign in as a regular user.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807