NodeJS authentication with Firebase

Karol Bilicki picture Karol Bilicki · Apr 16, 2017 · Viewed 29.5k times · Source

I would like to authenticate and keep sessions via NodeJS with Firebase. Client can't directly communicate with Firebase.

In short:

Client (browser) <----> NodeJs(firebase-admin) <----> Firebase

I created Firebase client in NodeJS, then I used login method:

var firebaseClient = require('firebase');
firebaseClient.initializeApp(config)
firebaseClient.auth().signInWithEmailAndPassword(req.body.email, req.body.password).catch(function(error){
    console.log(error);
})

and then I created route to check authenticated user:

app.get('/check',function(req,res){
    var user = firebaseClient.auth().currentUser
    console.log(user)
})

This method only allows me keep 1 previously logged user.

I would like to use firebase-admin, but I don't know how to keep session and authenticate users

Answer

Hiranya Jayathilaka picture Hiranya Jayathilaka · Apr 19, 2017

You can authenticate clients on their respective devices/browsers using the client SDK, and them get them to send an ID token to a backend service written using firebase-admin (the admin SDK). The admin SDK provides methods for validating ID tokens sent by clients: https://firebase.google.com/docs/auth/admin/verify-id-tokens