15
Published Mar 10, 2017

Using Firebase as an Authenticating Middleware in Express.js

Using Firebase as an Authenticating Middleware in Express.js

You might have heard about the simplicity of Firebase and how it's an all-in-one solution for database management, authenticating, and storage. Did you know that you can use Firebase as an authentication middleware without storing sessions in your database? Today, I will talk about writing a middleware for your express application using Firebase alone. Here are the steps required to create a middleware with Firebase:

Create an account on Google:

If you do not have an account on Google, you can create one here. After creating the account, head over to the Google Firebase Console to create a Google Firebase Console account if you don’t have one already. After creating an account, you will need to create a project in Firebase. Creating the project will give you a config object that allows you to connect your application to Firebase’s database, storage, and authentication services. Copy this object, and store the values in your environment variable. firebase-console-edited.png

Install Firebase in Node:

Install firebase in your node application by running npm install Firebase  --save. This will save Firebase in your application dependencies, in case you want to run it in another environment.

Create Firebase config object:

In your application middleware file or entry file, you will need to require Firebase and create the config object for the application. Your settings need to be created like so:

  var firebase = require('firebase');
  require('firebase/auth');
  require('firebase/database');
  // Initialize Firebase for the application
  var config = {
      apiKey: process.env.apiKey,
      authDomain: process.env.authDomain,
      databaseURL: process.env.databaseURL,
      storageBucket: process.env.storageBucket,
      messagingSenderId: process.env.messagingSenderId
    };

Initialize Firebase for your application:

After creating the config object and requiring Firebase and its services(database & authentication), you will need to initialize Firebase in your application like so:

firebase.initializeApp(config); 

Initializing Firebase in the middleware fileCreate for your route:

After initializing the application, create the middleware function that will be placed in your routes as needed: We will create a middleware in a file called auth.js, which will check if the user is authenticated or logged in and export it.

  module.exports = {
    isAuthenticated: function (req, res, next) {
      var user = firebase.auth().currentUser;
      if (user !== null) {
        req.user = user;
        next();
      } else {
        res.redirect('/login');
      }
    },
  }

Creating the middleware functionUse in a route:

Finally, after creating the middleware, you can use this middleware in a route and see that it works like so:

  var documents = require('../controllers/documents');

  const routes = (router, authenticate) => {
    // Get all documents
    router.get('/documents/', authenticate.isAuthenticated, documents.getAll);
  }

  module.exports routes;

You do not need to use a different package as an authenticating middleware to store sessions in your database. Feel free to reach out to me if you have any difficulties following this guide. I will respond as quickly as possible.


This post was originally posted by the author here. This version has been edited for clarity and may appear different from the original post.

DOM Testing React Applications with Jest
Handling AJAX in your React Application with Agility
How to Parse JSON into a C# Object