Author: Antonio Erdeljac
In this article you will learn how to handle authentication for your Node server using Passport.js. This article does not cover Frontend authentication. Use this to configure your Backend authentication (Generate token for each user & protect routes).
Keep in mind that if you get stuck on any step, you can refer to this GitHub repo.
In this article I will teach you the following:
- Handling protected routes
- Handling JWT tokens
- Handling unauthorised responses
- Creating a basic API
- Creating models & schemas
What is Passport.js?
Passport is authentication middleware for Node.js. As it’s extremely flexible and modular, Passport can be unobtrusively dropped into any Express-based web application. A comprehensive set of strategies supports authentication using a username and password, Facebook, Twitter, and more. Find out more about Passport here.
Creating our node server from scratch
Create a new directory with this “app.js” file inside:
We will install nodemon for easier development.
and then we will run our “app.js” with it.
$ nodemon app.js
Creating the user model
Create a new folder called “models”, and create the “Users.js” file inside that folder. This is where we will define our “UsersSchema”. We are going to use
Crypto to generate
salt from the received
password string. This will later be used to validate the user.
Let’s add our newly created model to “app.js”.
Add the following line to your “app.js” file after configuring
Create a new folder “config” with the “passport.js” file inside it:
In this file, we use the method
validatePassword that we defined in the
User model . Based on the result, we return a different output from Passport’s
Let’s connect “passport.js” to our “app.js” file. Add the following line below all
Routes and authentication options
Create a new folder called “routes” with the file “auth.js” inside it.
In this file we use the function
getTokenFromHeaders to get a JWT token that will be sent from the client side in the request’s headers. We also create an
auth object with
required properties. We will use these later in our routes.
In the same “routes” folder create an “index.js” file:
We now need an “api” folder inside the “routes” folder, with another “index.js” file inside it.
Now, let’s create the “users.js” file that we require in “api/index.js”.
First, we are going to create an optional auth route
‘/’ which will be used for new model creation (register).
router.post('/', auth.optional, (req, res, next) ...
After that, we are going to create another optional auth route
‘/login’ . This will be used to activate our passport configuration and validate a received password with email.
router.post('/login', auth.optional, (req, res, next) ...
Lastly, we will create a required auth route, which will be used to return the currently logged in user. Only logged in users (users that have their token successfully sent through request’s headers) have access to this route.
router.get('/current', auth.required, (req, res, next) ...
Let’s add our “routes” folder to “app.js”. Add the following line below our passport
I will be using Postman to send requests to our server.
Our server accepts the following body:
Creating a POST request to create a user
We will now use this token and add it to our “Headers” in Postman’s configuration.
And now let’s test our auth only route.
Creating a GET request to return the currently logged in user
Let’s try to do it without token in “Headers”.
Thank you for going through this tutorial. If you notice any errors please report them to me. If you got stuck on any step, please refer to this GitHub repo.