An image

Corporate Blogs

Authentication In MEAN Stack – A Quick Guide

E-Commerce Businesses Need a Mobile App

In this post, we will get to know how to authenticate in a MEAN stack application. The MEAN stack refers to javascript based technologies MongoDB, Express JS, Angular, and Node Js. Here, we are using angular version 9.  Our application has registration, login, and protecting only logged in users pages, managing sessions on the client side even after refreshing the pages and logout.

I hope this should be a good example of mean stack authentication projects. We are going to use bcrypt hashing for storing passwords, JWT for authenticating users’ token, angular interceptor, and router guard for managing sessions in client sides. 

You can also check out my repo to see my full code that this blog is going to be explained.


If you are going to practice the following code. Please ensure that you already have basic knowledge with mean stack and you have already installed Node Js, Express Js generator, MongoDB, and Angular.

Create an Express js application for server-side:

Now, we should create a server-side application using the express-generator command. If you are not already installed install it by “npm install -g express-generator”. Create an “express server –no-view” then move to the “server folder” now you can run the server by “npm run start”. This is a starter kit generated from the express-generator. Now we can start to code from here and copy the config code repo.

Mongoose Installation Command: 

MongoDB connection setup by mongoose: 

We will create a MongoDB connection by using the mongoose connect method.

Import this file on app.js and call the connect method. It will call the MongoDB connection on the app start.

Create Users Schema:

Now we need to set up the Mongoose users schema for MongoDB database setup. Create user files in the model’s folder (you need to create models folder manually) schema for registration and login set up.

Let’s create users schema:

I have also built custom static methods to getUser, saveUser, and password hashing using mongoose statics.  The mongoose pre hooks to capture the save query to hashing the password. Hashing the password is very important to protect the data(I have used bcrypt here).

So the next thing is we are going to write the routers to log in, register. If you use express-generator users, the router file will be available in routes by default. So you don’t need to create anything else to create users.js file routes. and add the following code. Here we are going to create APIs for Register, login, and other APIs related to Users.

Register API: 

You can see the following code whenever the user payload comes to our registration API. First, we need to check out the user-provided data in the correct format. Then to check if the provided email is already registered we are using Users.getUser method to check. If the email was not already registered we need to save the payload by Users.saveUser(new Users(user)). whenever you insert data it should have the specified schema. For that 

I am formatting by using the Users modal class.

Login API: 

First, we will see the login code in below here we are checking if an email is available then comparing the password finally giving access-token to access the secure pages. 

I have used JWT for user tokens we don’t need to maintain on the server-side. If you want to use any other methods also you can go with that.

Now we will create an API that only accesses logged in users. Let’s see.

Here we are using middleware to check the APIs come with a valid access token. Then decoded user data stores it in req.body.auth to get the user info in API handlers.

Create middlewares/auth.js and load the following code.

The server-side part is completed now we will see the client-side to authenticate the user and manage the user sessions. In this blog, I am going to explain it in Angular 9

but it’s also suitable for angular 5+.

Before going to you need to create the angular app. Create it by ng new command. If you are not already installed, install angular by using the following command. I have also using angular material here for building UI. The installation reference link is here. Angular material Installation guide

Then we need to create components for login, register, and users list page. Create it by the following command if you are not already familiar with this.

Then build UI for those components. I have created something simple. That’s available in my repo The link was already mentioned above. Here I will show the example code for login. Then it’s also similar to registration. I have used reactive form module for forms so you need to import ReactiveFormsModule.

Once after creating those components you need to declare the import dependency module in the app module and set the path in the router module. You can see the router guards  (canActivate: [AuthGuardService]) in this code. This is used to determine whether the path can load in view or not. 

The AuthGuardService service is used to protect the secured pages from unauthorized users and NoAuthGuardService used to when logged in users accessing the login and 

Registration page.

Create those files by the following command

You can copy the full source from my repo. I am just explaining the core part. When the token is not available in not allowing the navigation and returning to login. Try to navigate the user’s page before login. It will redirect to the login page.

Here, No Authguard service is just explaining the core part. When the token is available in not allowing the navigation and returning to users page. Once after login, try to log in it will redirect you to the users page.

The example code for the login component. 

Note, I am not showing error and error not handled correctly in the component.

So you can see the above code when the form values are valid and sent to the server if there is no error storing the access token in localStorage and navigating. You need to create an API service file to send the data.


Finally coming to the conclusion You can run the project from my repo and I hope you enjoyed the blog. Thanks for reading if you have any questions or found any suggestions, so write it in the comments section.

We, Agira technologies are a technology solution company with business services and domain solutions that support global clients who comprise the current world economy. Some of the exclusive services that we offer are web development, mobile app development, Blockchain, IoT, and DevOps Consulting.

Do you find it interesting? you might also like these articles. Top 10 Best Tech Companies For Employees To Work In The USA In 2020 and Top 10 IT Staffing and Recruiting Agencies in the USA.

If you have a business idea in your mind and in search of a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.

Looking for a Tech partner to dominate the digital world?
Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
A zealous full stack web developer with 3 years of professional experience. Hybrid application development and in developing Open-source projects are the best of my list. Out of passion ethical hacking is one of my hobbies. Yet, I am good at analyzing Web Security. And I teach machines with my expertise in Robotics and Machine learning.

Leave a Comment

You have an Idea. We have the Solution.

We help business evolve with lates technologies and infrastructures tailored to their needs and market trends.