Milkncookiez
Milkncookiez

Reputation: 7377

Authenticate node.js server with Firebase Realtime DB

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.

Upvotes: 0

Views: 1109

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598603

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.

Upvotes: 1

Related Questions