<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andres Gutirerez on Medium]]></title>
        <description><![CDATA[Stories by Andres Gutirerez on Medium]]></description>
        <link>https://medium.com/@agutierrezt?source=rss-c796476d1a5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GJz_aaSHffQETea2chzBKw.jpeg</url>
            <title>Stories by Andres Gutirerez on Medium</title>
            <link>https://medium.com/@agutierrezt?source=rss-c796476d1a5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 17:15:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@agutierrezt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A node implementation for refresh token with Redis]]></title>
            <link>https://medium.com/@agutierrezt/a-node-implementation-for-refresh-token-with-redis-a062020bbec0?source=rss-c796476d1a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/a062020bbec0</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[jwt]]></category>
            <dc:creator><![CDATA[Andres Gutirerez]]></dc:creator>
            <pubDate>Mon, 17 Jan 2022 18:12:55 GMT</pubDate>
            <atom:updated>2022-01-17T22:59:22.553Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="https://unsplash.com/photos/zE007SNgcdE" src="https://cdn-images-1.medium.com/max/1024/1*eShP9pgf0QvFR1Jh3UG-_w.jpeg" /><figcaption><a href="https://unsplash.com/photos/zE007SNgcdE">https://unsplash.com/photos/zE007SNgcdE</a><a href="https://unsplash.com/photos/EaB4Ml7C7fE?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditShareLink">?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditShareLink</a></figcaption></figure><p>In this article, I will walk through the steps to setup an <strong>nodejs</strong> application with <strong>express</strong> and <strong>redis</strong> to implement a simple <strong>jwt</strong> based authentication with refresh token, to facilite the process I will use <strong>docker</strong> to install and use <strong>redis</strong>.</p><h3><strong>Prerequisites</strong></h3><p>In order to take advantage of this tutorial is important to have a basic knowledge of <strong>javascript</strong> and <strong>express </strong>although the code is quite simple to understand, first we need to install some tools:</p><ul><li>Nodejs</li><li>Npm</li><li>Docker</li></ul><h3><strong>Redis installation</strong></h3><p>To install redis I will use docker, in the terminal we run the following command:</p><pre>docker run -d --name &lt;NAME&gt; -p 6379:6379 redis</pre><p>You can check if the installation was correct with the next command:</p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G3rG2xlGJw431FdLu-p9-A.png" /></figure><p>if the status is <strong>up, </strong>everything is configured and working correctly.</p><h3><strong>Express application setup</strong></h3><p>In order to create a <strong>nodejs</strong> project in our terminal, we need to be inside a directory, where the files will be created and run the following command:</p><pre>npm init</pre><p>Once the command is successful a file called <strong>package.json</strong> file will be created, then we will proceed to install some dependencies inside the root directory running the following commands:</p><pre>npm i express bcrypt cors helmet jsonwebtoken redis<br>npm i -D nodemon dotenv</pre><p>After run those command you get a file similar to next one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e03864e96680b112b7fd5747e7752b0/href">https://medium.com/media/5e03864e96680b112b7fd5747e7752b0/href</a></iframe><p>Then we will create a file in the root directory with the name of <strong>server.js</strong> and the following content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GxqI9_lndxilxK8viPw4IQ.png" /></figure><p>This is the basic configuration to create an express app to check that every thing is ok, we will run the next command:</p><pre>npm start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*VnMKGkTJmhOVWp-3Fx1H4w.png" /></figure><h3>Configure env values</h3><p>Now we will use a previous package installed called <strong>dotenv</strong> to set some environment variables that we will use in our app.</p><ul><li>in the root directory we create <strong>.env</strong> file with the following values:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WeghHM2uIzLVkuD6xta5qg.png" /></figure><ul><li>to load those variables we modify our <strong>server.js</strong> with the following lines of code:</li></ul><pre>import dotenv from &quot;dotenv&quot;;<br>dotenv.config();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LIXsS-4eU1x_o8cYViVvzw.png" /></figure><h3><strong>Folder structure</strong></h3><p>To make our code and project more clean we implement the following folder structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*pe7jNwS-gpTtyNlUIzlDAQ.png" /><figcaption>Folder structure</figcaption></figure><ul><li><strong>middlewares: </strong>We store express middlewares, those middlewares will be stored in different files based on their functions.</li><li><strong>routes: </strong>We store the different express routes, those routes will be stored in separate files based on the <strong>base path.</strong></li><li><strong>controllers: </strong>We store the different controllers of the application, we will have a separate file for each route file.</li><li><strong>services: </strong>We create a file for each service of the application, in those files we store custom functionality that can be used across the application for instance, the <strong>redis</strong> service to know if a refresh token was already used.</li><li><strong>errors: </strong>In this folder we store the custom errors, each custom error has a separate file, those errors are used to identify the kind of error generated and to add more value information to them.</li></ul><h3><strong>Errors</strong></h3><p>In the errors folder we are going to create a file called <strong>server.error.js</strong> with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85735474e9bb1c581d0b2693dcd7bcaf/href">https://medium.com/media/85735474e9bb1c581d0b2693dcd7bcaf/href</a></iframe><h3>Services</h3><p>In this folder we are going to create files to hold the business rules and logic of our project, we use the service layer pattern to follow the best practices of skinny controllers.</p><p>We are going to create the following files:</p><ul><li><strong>jwt.service.js: </strong>In this file will have a method to generate <strong>jwt</strong> tokens:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e33f975b6deb777c23f86a20135af2c8/href">https://medium.com/media/e33f975b6deb777c23f86a20135af2c8/href</a></iframe><ul><li><strong>auth.service.js: </strong>In this file will have methods related with authentication logic:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c2afba56cd4cac20df6c89350c89576/href">https://medium.com/media/7c2afba56cd4cac20df6c89350c89576/href</a></iframe><ul><li><strong>redis.service.js: </strong>In this fill will have the corresponding method to connect, set and get information from our <strong>redis</strong> instance:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54a62e8aa90590398e1fd6a42f06dffa/href">https://medium.com/media/54a62e8aa90590398e1fd6a42f06dffa/href</a></iframe><ul><li><strong>hello.service.ts: </strong>This file has a method which return the information of the user logged in:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea9a3489bd228d5d22ca5679a13170d5/href">https://medium.com/media/ea9a3489bd228d5d22ca5679a13170d5/href</a></iframe><h3><strong>Middlewares</strong></h3><p>Now we are going to create three additional files in the middlewares folder as follow:</p><ul><li><strong>auth.middleware.js: </strong>in this file will store an express middleware, which is responsable of validate if the authentication header is present and the value is a valid <strong>jwt</strong> token, additionally we made other checks as if the token type has the value of <strong>ACCESS:</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/483b1968630d993686c9a245d2d55594/href">https://medium.com/media/483b1968630d993686c9a245d2d55594/href</a></iframe><ul><li><strong>refresh.middleware.js: </strong>in this fill will store an express middleware which is responsable of validate if a refresh token is present and the value is a valid <strong>jwt</strong> token, additionally we made other checks as if the token type has the value of <strong>REFRESH:</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7e80c9579c808fc163365a9bd9ae694/href">https://medium.com/media/f7e80c9579c808fc163365a9bd9ae694/href</a></iframe><ul><li><strong>error.middleware.js: </strong>in this file will store a special kind of express middleware that is in charge of caught any error inside our application:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8834fd8221d7af1f9145d00415c93ae5/href">https://medium.com/media/8834fd8221d7af1f9145d00415c93ae5/href</a></iframe><h3><strong>Controllers</strong></h3><p>In this folder we are going to create a controller for each route file that our application has, the controllers be responsable of respond to the client and communicate with services to do the logic, for this example we are going to create two files as follow:</p><ul><li><strong>auth.controller.js: </strong>In this file we are going to create a file related to authentication logic, for this example we are going to create two methods one to simulate a login and another to generate new <strong>jwt</strong> tokens using refresh token:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/224e2c617d60832ff579add7ad6effe9/href">https://medium.com/media/224e2c617d60832ff579add7ad6effe9/href</a></iframe><ul><li><strong>hello.controller.js: </strong>This file will have a method in which the user need to be authenticated, the method is quite simple it only returns the information hold in the authentication token:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13766eb98eaca5bd0def1c85e144310a/href">https://medium.com/media/13766eb98eaca5bd0def1c85e144310a/href</a></iframe><h3>Routes</h3><p>Finally we are going to create each of the routes of our application and configure the express server to respond to them.</p><p>We are going to create the following files:</p><ul><li><strong>auth.routes.js: </strong>This file hold the routes related to authentication as login and refresh token:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d26fd0a443accd69e74f96a4be749277/href">https://medium.com/media/d26fd0a443accd69e74f96a4be749277/href</a></iframe><ul><li><strong>hello.routes.js: </strong>This file hold the routes when the user is authenticated:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f1428728fd4bbe0c5d8f390e4d6102a/href">https://medium.com/media/9f1428728fd4bbe0c5d8f390e4d6102a/href</a></iframe><p>Then we modify the <strong>server.js </strong>to configure the routes into our server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/902f177236dfc6a18d763f8f4a982500/href">https://medium.com/media/902f177236dfc6a18d763f8f4a982500/href</a></iframe><h3>Test our application</h3><p>Finally, to test our app and check that everything is working properly, we need to run our app with the next command:</p><pre>npm run</pre><p>Then we can use the following <a href="https://www.getpostman.com/collections/338a9c309d6045d3368a">postman collection</a> to test the different endpoints, also the tokens can be verified through this <a href="https://jwt.io/">page</a>.</p><p>The completely code can be found in this <a href="https://github.com/Andres930410/nodejs-jwt-refesh-token">link</a>.</p><p>This is for now, I hope you enjoy the tutorial and your learning process to became in a proficient backend developer with express will be full of fun.</p><p><a href="https://www.linkedin.com/in/andres-gutierrez-2587b2ab/">Andres Gutierrez</a> is a Colombian senior backend and android developer, co-founder and currently CEO at <a href="https://www.linkedin.com/company/slabcode/mycompany/?viewAsMember=true">SLABCODE</a>, with more than 6 years of experience developing highly-scalable and high performance applications using different technologies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a062020bbec0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>