Build An Authentication system with node, express, and MongoDB
Tags: mongodb, node, javascript
Published: 2019-11-19 08:37:50

**O**ver the years, I have been able to adopt an interesting approach to register a user as well as log in the user. Am delighted to share a few decision that has worked for me and I find useful and worthwhile. And I hope to get comments and more insight from you guys to further help me learn and become better. Yes, we are all learning.

**What we want to do.**

I will assume you have an existing understanding of javascript, node js, express, and MongoDB. we want to set up a platform whereby a user can register as well as login to there account

**Main Dependencies**

1. Nodejs must be installed
2. Mongo DB
3. Passport
4. Babel
5. JSON WEB TOKEN

**Overview of dependencies, project structuring.**

The src folder contains the source file of the project. the purpose of each of the subfolders will be explained subsequently.

![Screen Shot 2019-10-24 at 9.08.20 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571947740154/D-uaQyarg.png)

**The package.json file and the basic dependencies**

![Screen Shot 2019-10-24 at 9.48.26 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571950125016/MxBKrSJMv.png)

A config folder to help specify variables required [click here](https://www.npmjs.com/package/config) to have a better understanding of how the environment variables are loaded. here is the default config file.

![Screen Shot 2019-10-24 at 9.53.09 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571950409851/OgYPsXGbJ.png)

**S**erver.js file 
 Where we will be starting our development server

![Screen Shot 2019-10-24 at 9.56.48 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571950624496/GL4MR_Jyq.png)

On a successful server setup in your console, you should have this logged.

![Screen Shot 2019-10-24 at 9.59.50 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571950803510/b_ni07AAr.png)

**N**ow that the server is up and running let write some code, let create a register model (create a register.model .js file) and put in the following


![Screen Shot 2019-10-24 at 10.02.33 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571950970346/oG9FR3ZVa.png)

**N**ow let create our register controller (create a register.controller.js file)

![Screen Shot 2019-10-24 at 10.05.06 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951120621/Wzwti61E2.png)


![Screen Shot 2019-10-24 at 10.05.43 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951156289/p4eheU-re.png)

**Now** let create our route 

![Screen Shot 2019-10-24 at 10.10.48 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951470785/CClGCrI7q.png)

Guys, we are still on track now that we have a registered user let create our Login controller and route (create a login.controller.js file)

![Screen Shot 2019-10-24 at 10.15.00 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951715592/M_BR_e2ql.png)
 

![Screen Shot 2019-10-24 at 10.15.32 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951748342/j2LeLOe0x.png)

**Let** create our login route(create a login.route.js file)

![Screen Shot 2019-10-24 at 10.17.41 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571951872474/3nGXAg-Ac.png)

Guys now that we are done let test what we've  done so far :
**U**sing Postman (Create a user).

![Screen Shot 2019-10-24 at 10.28.47 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571952583735/oPLh2Kew-.png)
 
![Screen Shot 2019-10-24 at 10.30.50 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571952671289/YCbufzyAW.png)

Let try to login with our Email and password :

![Screen Shot 2019-10-24 at 10.32.25 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571952763922/kOmx-XUNq.png)

![Screen Shot 2019-10-24 at 10.33.03 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1571952800269/rWhgmymws.png)

Wow we finally built An Authentication system with node, express, and MongoDB  thank you for your patience 
 [Source code](https://github.com/waletayo/Build-An-Authentication-system-with-node-express-and-MongoDB-)