JAVASCRIPT

TOKEN BASED AUTHENTICATION FOR MEAN STACK APPS – PART 1


Creating independent frontend and backend applications makes scalability easy. Building your backend as an API and consuming it with frontend components makes development easily maintainable and allows distribution across different servers when your app grows very large.

SETTING UP YOUR ENVIRONMENT

Today, we’ll build a simple app that demonstrates how to implement token-based authentication in mean stack apps. This is one way of going about it amidst many others.

MEAN – Mongodb( Our Database ), Express ( Node.js Framework ), AngularJs ( Frontend Framework ), Nodejs( Server-side Language )

Let’s Jump Right In

Let’s create a new Node.js application called auth-meanapp. Make sure you have npm installed already. Create a directory named auth-meanapp, this is where our application will reside.

Now, create a package.json file by running this command like so:

Go through the instructions, fill the required details. name, version, description, entry point, test command, git repository, keywords, author, license . It’s actually optional, you could just press enter continuously to skip it till it’s completed.

Then run the following command from your terminal like so:

express –  Fast minimalistic web framework for node.js

mongoose – It is a MongoDB object modeling tool designed to work in an asynchronous environment.

morgan – node.js module that enables detailed logging

dotenv – loads environment variables from .env in to ENV (process.env).

lodash – node.js module for working with data structures and functions

cors – node.js module for allowing cross origin request on your backend

gravatar – node.js module for fetching avatars from emails during user sign-ups

jsonwebtoken – node.js module for implementing json web tokens

body-parser – node.js middleware for parsing body form requests

bcrypt – A hashing library for node.js

save is to ensure that it’s details are saved in our package.json file.

Your package.json file should look something similar to  this:

This is the folder structure of our app:

auth-meanapp

The next step is to create  .env  file so that node.js can properly pull environment variables for the app.

My .env file looks like this:

.env

Note: Make sure you have mongodb installed on your system.

Next, let’s create a server.js file in the root of our folder. This is the main entry and starting point for our application.

server.js

secrets.js

 

testdb.js

 

tokenMiddleware.js

routes.js

If you have created all the files mentioned above, run the application like so:

NOTE: Install nodemon globally, if you don’t have it..run npm install nodemon -g on your terminal.

nodemon watches for file changes and automatically restarts your node application.

The application would be served on port 3000 and it’ll show an empty webpage.
Screen Shot 2015-07-02 at 11.22.47 PM

Thanks for reading, we’ll continue in the next post tomorrow! :)

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.