Using Firebase as an Authenticating Middleware in Express.js

Victor Nwaiwu
3 min readMar 8, 2017

--

You must have heard of the simplicity of Firebase and how it comes as an all in one solutions for database management, authenticating and storage. Do you know that you can use Firebase as an authentication middleware and you will not need to store sessions in your database anymore? 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 and create an account if you don’t have one. 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 concole config settings
  • 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 that will be used to run the application. Your settings need to be created like so:
requiring firebase and setting the config in the middleware file
  • 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:
initializing firebase in the middleware file
  • Create the middleware for your route: after initializing the application it is now time to create the middleware function that will be placed in your routes as needed: We will create a middleware in a file called ```auth.js ``` that checks if the user is authenticated or logged in and export it.
creating the middleware function
  • Use the middleware in a route: Finally, after creating the middleware, you can use this middleware in a route and see that it works like so:
route file where the middleware is used

You do not need to use a different package as an authenticating middleware and 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.

--

--