Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS authentication with Firebase

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

like image 391
Karol Bilicki Avatar asked Apr 16 '17 15:04

Karol Bilicki


People also ask

How does node js use authentication with Firebase?

In order to test this, we will have to use Firebase Auth to login a user to our front end, get a AuthToken for that user, then send that token to our server and use Firebase Admin to verify that token. So both the frontend and backend need to be using Firebase.

Can Firebase be used for authentication?

In the present era, user authentication is one of the most important requirements for Android apps. It is essential to authenticate users, and it is much harder if we have to write all this code on our own. This is done very easily with the help of Firebase.

CAN node js work with Firebase?

Firebase has a real-time database feature that allows easy creation of real-time applications such as chat apps. To get started, let's begin by creating a Node. js application using npm init and add the following packages: firebase : this will be used to access and perform actions to our firebase real-time database.


1 Answers

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

like image 118
Hiranya Jayathilaka Avatar answered Oct 04 '22 05:10

Hiranya Jayathilaka