Open In App

Firebase (sign in with Google) Authentication in Node.js using Firebase UI and Cookie Sessions

Last Updated : 26 Aug, 2020
Like Article

Firebase Authentication provides the backend services that are easy-to-use SDKs and ready-made UI libraries to authenticate users to your app.

Prerequisites: Basic knowledge of node, JavaScript and firebase.

Setup: First we need to create a Firebase Project, head over to firebase Console and create a new project. 

I am going to name it SignInWithGoogle

Click on the web to create a web app.

Go to Firebase Settings>Service Account >Generate a new Key. This Key should be remain private, It is advised to keep this in environment variables.

Now go to Authentication tab and turn on Sign in with Google.

Now Create a new project having success.html (with a simple anchor tag, directing to “/ ” root ) and login.html [leave a blank division with id “firebaseui-auth-container”, the place where Firebase UI will be Initialized]

Change console directory to root of your project type by using the following commands in the console

$npm init
$npm install express firebase-admin cookie-parser https fs

Note: The last two packages are only needed if you wanna save cookies in local-host, however, if you will be running a backend on https then there is no need.

const express = require("express");
const admin = require("firebase-admin");
const cookieParser = require("cookie-parser");
const https = require('https');
const fs = require('fs');
const app = express();
    + "qhkiG9w0BAQE--your key here--+1d4\n--END"
    + " PRIVATE KEY-\n";
    credential: admin.credential.cert({
        "private_key": key.replace(/\\n/g, '\n'),
        "client_email": "YOUR CLIENT EMAIL HERE",
        "project_id": "YOUR PROJECT ID "
app.get('/', (req, res) => {
   res.sendFile(__dirname +'/login.html');  
app.get('/logout', (req, res) => {
app.get('/success', checkCookie, (req, res) => {
    res.sendFile(__dirname + '/success.html');
    console.log("UID of Signed in User is" 
            + req.decodedClaims.uid);
    // You will reach here only if session
    // is working Fine
app.get('savecookie', (req, res) => {
    const Idtoken=req.query.token;
    setCookie(Idtoken, res);
// Saving cookies and verify cookies
// Reference : 
function savecookie(idtoken, res) {
    const expiresIn = 60 * 60 * 24 * 5 * 1000;
    admin.auth().createSessionCookie(idtoken, {expiresIn})
    .then((sessionCookie) => {
       const options = {maxAge: expiresIn, 
                httpOnly: true, secure: true};
        .then(function(decodedClaims) {
    }, error => {
        res.status(401).send("UnAuthorised Request");
function checkCookie(req, res, next) {
    const sessionCookie = req.cookies.__session || '';
        sessionCookie, true).then((decodedClaims) => {
            req.decodedClaims = decodedClaims;
        .catch(error => {
           // Session cookie is unavailable or invalid. 
           // Force user to login.

Here is app.js file, If you look it closely, you will find that there is no port listening to our request. This is where we need those two node modules.

Most browsers does’t allow you to save cookies in local host, that’s why we will setup HTTPS connection for our localhost.

Make sure you are in root directory of project.

Open bash and type

$openssl req -nodes -new -x509 -keyout server.key -out server.cert

Two files (server.key and server.cert) will be generated.

Add the following code to app.js file-

    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.cert')
  }, app)
  .listen(3000, function () {
    console.log('listening on port 3000!'
      + ' Go to https://localhost:3000/')

Load this script before body tag in login page

<!-- Firebase Package-->
<script src=
<!-- Loads the login UI elements-->
<script src=
<link type="text/css" rel="stylesheet" href=

Load this script in login page after body tag (fill the config data that you got from firebase console).

var config = {
    apiKey: "YOUR_KEY",
    authDomain: "YOUR_DOMAIN",
    databaseURL: "YOURURL",
    projectId: "--",
    storageBucket: "",
    messagingSenderId: "",
    appId: ""
// FirebaseUI config.
var uiConfig = {
    signInOptions: [
        // Google sign in option
    // Terms of service url/callback.
    tosUrl: '<your-tos-url>',
    // Privacy policy url/callback.
    privacyPolicyUrl: function () {
    callbacks: {
        signInSuccess: function (user, 
            credential, redirectUrl) {
                // User successfully signed in.
                user.getIdToken().then(function (idToken) {
                    window.location.href = 
                        '/savecookie?idToken=' + idToken;
                }).catch(error => {
// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());
// The start method will wait until the DOM is loaded.
ui.start('#firebaseui-auth-container', uiConfig);

Now hit save and run the command   $node app.js

Now go to https://localhost:3000/ and sign in, then close the tab or browser and type https://localhost:3000/success, you will see that you are not redirected to the sign-in page again, instead, you are taken to the success page.

Note: Here it didn’t ask me to select which account to login with the app because I was signed in with only one account, in-case you have signed in with multiple accounts, it will ask you to choose an account to proceed with.

Download My Completed project in case of any error you face or write in the comment


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads