<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raoul Vandevelde</title>
    <description>The latest articles on DEV Community by Raoul Vandevelde (@rallofield).</description>
    <link>https://dev.to/rallofield</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1103726%2F46a70a45-3df3-4e66-a118-b5e2d584c4ad.jpeg</url>
      <title>DEV Community: Raoul Vandevelde</title>
      <link>https://dev.to/rallofield</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rallofield"/>
    <language>en</language>
    <item>
      <title>Auth, Auth, Auth?</title>
      <dc:creator>Raoul Vandevelde</dc:creator>
      <pubDate>Sun, 18 Jun 2023 19:43:34 +0000</pubDate>
      <link>https://dev.to/rallofield/auth-auth-auth-2h3m</link>
      <guid>https://dev.to/rallofield/auth-auth-auth-2h3m</guid>
      <description>&lt;h5&gt;
  
  
  Written by &lt;a href="https://github.com/RalloField"&gt;Raoul Vandevelde&lt;/a&gt;
&lt;/h5&gt;




&lt;h3&gt;
  
  
  Middlewares in Node.JS
&lt;/h3&gt;

&lt;p&gt;During my training at &lt;strong&gt;BeCode&lt;/strong&gt; and a dive into the world of building &lt;strong&gt;API&lt;/strong&gt;'s with &lt;strong&gt;Express&lt;/strong&gt;, it was time to build an API ourselves.&lt;br&gt;
The exercise consisted of making a webshop API of which we could choose the subject.&lt;br&gt;
Ofcourse, this came with a lot of questions like, how would a user experience this in their browser? Would they be able to add, change and delete things from the database at will?&lt;/p&gt;

&lt;p&gt;That's something an e-shop owner would not want, right? &lt;br&gt;
Our goal, after having made all the necessary controllers and database connections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make &lt;strong&gt;authentication&lt;/strong&gt; and &lt;strong&gt;authorization&lt;/strong&gt; middlewares so only those who are authorized by the admin (in this case, the shop owner), have access to the specified functions in the controllers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Setup and context
&lt;/h3&gt;

&lt;p&gt;A bike-shop owner has asked you to create an API in which he can add his collection of bikes, to sell on his website.&lt;/p&gt;

&lt;p&gt;Let's just asume you have created your file-structure and are ready to create some middlewares!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hells yea! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GZVPdyhV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d8bructfccral0d227p.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GZVPdyhV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d8bructfccral0d227p.gif" alt="hold_your_horses gif" width="276" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hold your middle-horses - some developer &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's first go into something called the &lt;a href="https://jwt.io/introduction"&gt;JWT or JSON Web Tokens&lt;/a&gt;, which we will need to securely transmit our information to authenticate. Be sure to read up on it before continuing. &lt;/p&gt;

&lt;p&gt;We define our JWT_ACCESS_TOKEN in our .env file where we will assign to it a digital key.&lt;/p&gt;

&lt;p&gt;For Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JWT_ACCESS_TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;YRskPK2K5CoW0lOp4Fl9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having done this we will install the package needed to work with JWT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npm install jsonwebtoken
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Ready? Authenticate, Authorize, Set. Go!
&lt;/h3&gt;

&lt;p&gt;Let's get to work!&lt;br&gt;
Make a folder in your structure which will contain all your middlewares.&lt;br&gt;
No need to think deeply about the name, just call it 'Middlewares'.&lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;admin_authentication.middleware.js&lt;/code&gt; in the new folder you just created, and require at the top the following packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dotenv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the admin authentication we will check two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is the user logged in?&lt;/li&gt;
&lt;li&gt;If so, is the logged in user an admin?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get into the first point.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logged in?
&lt;/h4&gt;

&lt;p&gt;We will check if the user accessing the page is logged in or not.&lt;br&gt;
This first part will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authenticateAdmin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Extract the access token from the request cookie&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webshop.process&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// If access token is not present, user is not authenticated&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unauthorized&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Let's break down the code shall we?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DtBAYk3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6i2prq5pj2s4sdvuand.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DtBAYk3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6i2prq5pj2s4sdvuand.gif" alt="break_it_down" width="220" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The function defined takes three parameters into account: &lt;code&gt;req&lt;/code&gt; , &lt;code&gt;res&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt;. Next is used to call the next middleware or route handles when the middleware has passed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We extract the access token from the cookie with &lt;code&gt;req.cookies['webshop.process']&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: the &lt;code&gt;['webshop.process']&lt;/code&gt; is specific to what we are doing here and should be updated to the name you used in your login- or sessionscontroller when signing the cookie.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In the if statement we check if the access token is present or not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not, would mean they are not logged in and as such not authenticated to utilize the functionality being called in the routes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We respond with a &lt;code&gt;401 Unauthorized&lt;/code&gt; message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If they are indeed logged in, we will continue with the next part of the function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Are they admin?
&lt;/h4&gt;

&lt;p&gt;After having checked if they are logged in, let's check if they are an admin or not.&lt;/p&gt;

&lt;p&gt;We consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// Verify the access token&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JWT_ACCESS_TOKEN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is_admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Check if the user is an admin&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is_admin&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Forbidden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});}&lt;/span&gt;
        &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;is_admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is_admin&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Attach the decoded user data to the request object&lt;/span&gt;
&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="c1"&gt;// Proceed to the next middleware or route handler&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the access token is indeed present, the function will verify it with &lt;code&gt;jwt.verify()&lt;/code&gt; -&amp;gt; this will pass the token and the JWT digital secret (from the .env file) with &lt;code&gt;process.env.JWT_ACCESS_TOKEN&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If this verification succeeds, it checks if the decoded payload has &lt;code&gt;is_admin&lt;/code&gt; equal to 1 (make sure your &lt;code&gt;is_admin&lt;/code&gt; column is a boolean type, where 1 makes sure it is an admin).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this is not the case, the function will respond with a &lt;code&gt;403 Forbidden&lt;/code&gt; status and a JSON message if needed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, the &lt;code&gt;is_admin&lt;/code&gt; property is being attached to the &lt;code&gt;req.user&lt;/code&gt; if the user is indeed an admin and the &lt;code&gt;next()&lt;/code&gt; function is called to proceed to the next middleware or route handler.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// If access token verification fails, user is not authenticated&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unauthorized&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Because we used &lt;code&gt;try&lt;/code&gt; in our function we should also use a &lt;code&gt;catch(err)&lt;/code&gt; to make sure something is returned when the verification fails&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Final touches before testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure that you export the function by putting this line below on your page:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;module.exports = authenticateAdmin&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access your routes file where you want to use the middleware and require it at the top:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const authenticateAdmin = require('../../MIDDLEWARES/admin_authentication.middleware');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Depending on your filestructure this path will vary.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the middleware(s) in the chronological order:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/admins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;authenticateAdmin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;adminController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAdmins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This route will first check the authenticateAdmin middleware, before it executes the function specified behind it coming from the controller. In this case the admin must first be logged in and authorized before it can see a list of all the admins in the database.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Test
&lt;/h3&gt;

&lt;p&gt;To test our middleware but also to test your routes and methods in your controller, we will use a program called Postman. Postman is a handy little tool that will help us check if the middleware we just made really works.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Pro-Tip: To read more about how to use Postman in your project, &lt;a href="https://dev.to/biceschembri/how-i-used-postman-to-test-my-express-api-1bk0"&gt;this article&lt;/a&gt; will get you a long way!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Test route without being logged in:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MFjdp9Md--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dme0wkgwqoepxjn85tsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MFjdp9Md--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dme0wkgwqoepxjn85tsg.png" alt="Forbidden_access" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in with an admin account:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7vzcij1_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq2s2e4vghmdhktg8gcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7vzcij1_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq2s2e4vghmdhktg8gcs.png" alt="login" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test it again&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lx-ulgtf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju0zifhmn5bo27sfhz98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lx-ulgtf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju0zifhmn5bo27sfhz98.png" alt="authenticated" width="800" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the middleware works for this route. But you can now apply it also for other routes where only an admin would have access to.&lt;/p&gt;




&lt;h4&gt;
  
  
  Thanks for reading!
&lt;/h4&gt;

&lt;p&gt;I sincerely hope this has helped you out. I will keep updating this article if I find more during my middlewaring during my journey in web-development.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mTfA2iDd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivvhs4ujvkfa5yvtot57.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mTfA2iDd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivvhs4ujvkfa5yvtot57.gif" alt="youshallnotpass" width="498" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>api</category>
      <category>security</category>
    </item>
  </channel>
</rss>
