Member-only story
Authentication with the MERN Stack | Part II
In the last article, we started to build an authentication system with the MERN stack, which will eventually allows us to use our Express/NodeJS backend to communicate with our React application, and manage state and users with the Redux Library. We initialized our application, installed the necessary packages and dependencies, adjusted the package.json, and configured our external database through MongoDB.
In this article, we will build out the User model, create our routes, and develop the ability to register a user. Let’s get started.
Routes
In the root directory, create a folder called ‘Routes.’ Within this folder, we will have two files: auth.js and users.js. I highly recommend testing the routes using Postman.
Auth.js
In this file, we will define two routes: a GET route, and a POST route. Our GET route will retrieve our logged-in user, whereas our POST route will authenticate a new user, and retrieve a JWT (token). These routes will simply be defined here, and we will add the functionality later on.
At the top of the file, require Express, and create a constant called router, which will be the Router functionality built into Express.
Our GET route will go to the path (‘/api/auth’), but we will create a route path in Server.js that will include (‘/api/auth’). So, we simple want to make this request to the root path (‘/’). The access to this route will be Public, because…