{"id":4003,"date":"2019-01-04T18:26:29","date_gmt":"2019-01-04T12:56:29","guid":{"rendered":"https:\/\/code4developers.com\/?p=4003"},"modified":"2019-01-04T17:36:37","modified_gmt":"2019-01-04T12:06:37","slug":"node-express-jwt-authentication-using-jsonwebtoken-and-bcryptjs","status":"publish","type":"post","link":"https:\/\/code4developers.com\/node-express-jwt-authentication-using-jsonwebtoken-and-bcryptjs\/","title":{"rendered":"Node Express-JWT Authentication Using jsonwebtoken and bcryptjs"},"content":{"rendered":"<p>Throughout this tutorial, we&#8217;ll be learning how you can create a JWT authentication server with Node.js and Express.js using some popular libraries like:<!--more--><\/p>\n<ul>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/express\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #00ffff;\"><span style=\"color: #339966;\">express<\/span><\/span><\/a><\/li>\n<li><span style=\"color: #339966;\"><a style=\"color: #339966;\" href=\"https:\/\/www.npmjs.com\/package\/jsonwebtoken\" target=\"_blank\" rel=\"noopener\">jsonwebtoken<\/a><\/span><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/bcryptjs\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #00ffff;\"><span style=\"color: #00ff00;\"><span style=\"color: #339966;\">bcryptjs<\/span><\/span><\/span><\/a><\/li>\n<\/ul>\n<p>For making things simple, we&#8217;ll be using a SQLite database but this can be easily changed to use fully-fledged database management systems like MySQL.<\/p>\n<h4 id=\"prerequisites\">Prerequisites<\/h4>\n<p>In this tutorial, you need to have:<\/p>\n<ul>\n<li>A development machine with a recent version of Node.js and NPM installed,<\/li>\n<li>And a basic knowledge of JavaScript.<\/li>\n<\/ul>\n<h4 id=\"creating-the-express-js-server\">Creating the Express.js Server<\/h4>\n<p>Let&#8217;s start by creating our Node project. In your terminal run the following commands to generate a <span style=\"color: #ff6600;\">package.json<\/span> file inside the project&#8217;s folder:<\/p>\n<pre class=\"theme:dark-terminal lang:ps decode:true\"><span style=\"color: #ff6600;\">$ mkdir express-auth-project\r\n$ npm init -y<\/span><\/pre>\n<p>This command will generate an <span style=\"color: #ff6600;\">express-auth-project\/package.json<\/span> file with default content:<\/p>\n<pre class=\"theme:github lang:js decode:true\"> {\r\n    \"name\": \"express-auth-project\",\r\n    \"version\": \"1.0.0\",\r\n    \"description\": \"\",\r\n    \"main\": \"index.js\",\r\n    \"scripts\": {\r\n      \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"\r\n    },\r\n    \"keywords\": [],\r\n    \"author\": \"\",\r\n    \"license\": \"ISC\"\r\n  }<\/pre>\n<p>From the main property, you can see that we need to create an index.js file that will be the entry point of our application i.e the first file that will be executed by Node. So simply create the file in your project&#8217;s folder:<\/p>\n<pre class=\"theme:dark-terminal lang:ps decode:true\">$ touch index.js<\/pre>\n<p><em><strong>[blockquote]Note: Please note that you can use any name for your entry point file such as server.js instead of index.js but you should set that accordingly in the main property in your package.json file.[\/blockquote]<\/strong><\/em><\/p>\n<p>Before adding any code inside the <span style=\"color: #ff6600;\">index.js<\/span> file, we first need to install a bunch of packages from npm such as <span style=\"color: #ff6600;\">express<\/span>, <span style=\"color: #ff6600;\">sqlite3<\/span>, <span style=\"color: #ff6600;\">jsonwebtoken<\/span>, <span style=\"color: #ff6600;\">bcryptjs<\/span> and <span style=\"color: #ff6600;\">bodyparser<\/span>.<\/p>\n<p>Go to your terminal and run the following command to install the required libraries from npm:<\/p>\n<pre class=\"theme:dark-terminal lang:ps decode:true\">$ npm install --save express body-parser sqlite3 bcryptjs jsonwebtoken<\/pre>\n<p>At the time of this writing the following versions will be installed:<\/p>\n<p><span style=\"color: #ff6600;\">bcryptjs@2.4.3<\/span><br \/>\n<span style=\"color: #ff6600;\">sqlite3@4.0.4<\/span><br \/>\n<span style=\"color: #ff6600;\">body-parser@1.1<\/span><br \/>\n<span style=\"color: #ff6600;\">jsonwebtoken@8.4.0<\/span><br \/>\n<span style=\"color: #ff6600;\">express@4.16.4<\/span><\/p>\n<p>The command will create a <span style=\"color: #ff6600;\">node_module<\/span>s folder inside your project&#8217;s folder where the packages and their dependencies are all installed. Since we&#8217;ve added the <span style=\"color: #ff6600;\">&#8211;save<\/span> option, the command will also update the the package.json file with these dependencies which will enable anyone who cloned the project to install the same packages you&#8217;ve installed with the previous command by simply running the <span style=\"color: #ff6600;\">npm install<\/span> command from the root of the project&#8217;s folder where<span style=\"color: #ff6600;\"> package.json<\/span> exists.<\/p>\n<p>This is the content of <span style=\"color: #ff6600;\">package.json<\/span> at this point:<\/p>\n<pre class=\"theme:github lang:js decode:true\">{\r\n    \"name\": \"express-auth-project\",\r\n    \"version\": \"1.0.0\",\r\n    \"description\": \"\",\r\n    \"main\": \"index.js\",\r\n    \"scripts\": {\r\n      \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"\r\n    },\r\n    \"keywords\": [],\r\n    \"author\": \"\",\r\n    \"license\": \"ISC\",\r\n    \"dependencies\": {\r\n      \"bcryptjs\": \"^2.4.3\",\r\n      \"body-parser\": \"^1.18.3\",\r\n      \"express\": \"^4.16.4\",\r\n      \"jsonwebtoken\": \"^8.4.0\",\r\n      \"sqlite3\": \"^4.0.4\"\r\n    }\r\n  }<\/pre>\n<p>After installing the required packages, you can now proceed by creating your Express server. Open the <span style=\"color: #ff6600;\">index.js<\/span> file and add the following code:<\/p>\n<pre class=\"theme:github lang:js decode:true\">\"use strict\";\r\nconst express = require('express');\r\nconst bodyParser = require('body-parser');\r\n\r\nconst app = express();\r\nconst router = express.Router();\r\n\r\nrouter.use(bodyParser.urlencoded({ extended: false }));\r\nrouter.use(bodyParser.json());<\/pre>\n<p>We first require the <span style=\"color: #ff6600;\">express<\/span> and <span style=\"color: #ff6600;\">body-parser<\/span> modules, new we create an Express application and en Express router. Finally we added body parser middlewares that will allow us to get parse JSON data from the request body.<\/p>\n<p>Next let&#8217;s define two<span style=\"color: #ff6600;\"> \/login<\/span> and<span style=\"color: #ff6600;\"> \/register<\/span> routes which both should accept a POST request from clients:<\/p>\n<pre class=\"theme:github lang:js decode:true\">router.post('\/register', (req, res) =&gt; {\r\n    res.status(200).send({ access_token: '' });\r\n});\r\n\r\nrouter.post('\/login', (req, res) =&gt; {\r\n    res.status(200).send({ access_token: '' });\r\n});<\/pre>\n<p>We use the <span style=\"color: #ff6600;\">post()<\/span> method of the router object to create a route that accepts a POST request. The method takes the path as the first parameter and a function to process the request as the second parameter. This function is passed two objects:<\/p>\n<ul>\n<li><strong><span style=\"color: #ff6600;\">req<\/span> <\/strong>that represents the request sent from the client,<\/li>\n<li><strong><span style=\"color: #ff6600;\">res<\/span><\/strong> that represents the response that will be sent to the client.<\/li>\n<\/ul>\n<p>Both these two objects contains values and methods to work with requests and responses.<\/p>\n<p>In the body of functions, we simply set the <strong>200 OK<\/strong> status on the response and send it the client with an <span style=\"color: #ff6600;\">{ access_token: &#8216; &#8216; }<\/span> body.<\/p>\n<p>This will return the following HTTP response to the client:<br \/>\n<span style=\"color: #ff6600;\"><span class=\"err\">HTTP\/<\/span><span class=\"mf\">1.1<\/span> <span class=\"mi\">200<\/span> <span class=\"err\">OK<\/span><\/span><br \/>\n<span style=\"color: #ff6600;\"><span class=\"err\">Content-Type:<\/span> <span class=\"err\">application\/json<\/span><\/span><\/p>\n<p><span class=\"p\" style=\"color: #ff6600;\">{<\/span><br \/>\n<span style=\"color: #ff6600;\"><span class=\"nt\">\u00a0\u00a0\u00a0\u00a0 &#8220;access_token&#8221;<\/span><span class=\"p\">:<\/span> <span class=\"s2\">&#8220;&#8221;<\/span><\/span><br \/>\n<span class=\"p\" style=\"color: #ff6600;\">}<\/span><\/p>\n<p>For now the access token is empty so we&#8217;ll next change that to actually register, login and return an actual access token that will be used to authenticate the clients.<\/p>\n<p>Next, add the following code to set up the router and run Express server:<\/p>\n<pre class=\"theme:github lang:js decode:true\">app.use(router);\r\nconst port = process.env.PORT || 3000;\r\nconst server = app.listen(port, () =&gt; {\r\n    console.log('Server listening at http:\/\/localhost:' + port);\r\n});<\/pre>\n<p>At this point, you can run your server using the following command from the root of your project:<\/p>\n<pre class=\"theme:dark-terminal lang:ps decode:true\">$ node index.js<\/pre>\n<p>In your terminal the <span style=\"color: #ff6600;\">Server listening at http:\/\/localhost:3000<\/span> will be displayed which means your server is up and running and available from the <span style=\"color: #ff6600;\">http:\/\/localhost:3000<\/span> address.<\/p>\n<p>You&#8217;ll be able to send POST requests to the <span style=\"color: #ff6600;\">http:\/\/localhost:3000\/register<\/span> and <span style=\"color: #ff6600;\">http:\/\/localhost:3000\/login<\/span> endpoints to respectively register and login users.<\/p>\n<p>Let&#8217;s also look at how we can create a route that accepts a GET request and returns a response to the client. In the <span style=\"color: #ff6600;\">index.js<\/span> file, add the following route:<\/p>\n<pre class=\"theme:github lang:js decode:true\">router.get('\/', (req, res) =&gt; {\r\n    res.status(200).send('This is an authentication server');\r\n});<\/pre>\n<p>This will allow you to visit the <span style=\"color: #ff6600;\">http:\/\/localhost:3000\/<\/span> from your web browser. In your browser, you&#8217;ll see the <strong>This is an authentication server<\/strong> message.<\/p>\n<h3 id=\"adding-a-sqlite-database\">Adding a SQLite Database<\/h3>\n<p>To be able to register and login users in our application we need a way to persist users in our database. For this matter, we&#8217;ll use SQLite, a file based database that can be quickly created without installing a database management system like MySQL.<\/p>\n<p>Open the index.js file and require the sqlite3 package you&#8217;ve previously installed using the following code:<\/p>\n<pre class=\"theme:github lang:js decode:true\">const sqlite3 = require('sqlite3').verbose();<\/pre>\n<p>Next create a database object using:<\/p>\n<pre class=\"theme:github lang:js decode:true\">const database = new sqlite3.Database(\".\/my.db\");<\/pre>\n<p>Put this code at the beginning of your <span style=\"color: #ff6600;\">index.js<\/span> file after the require method and before registering the routes.<\/p>\n<p>Next, add three methods to create the <span style=\"color: #ff6600;\">users<\/span> table where users are persisted, create a user in the database and find a user by its email in the database:<\/p>\n<pre class=\"theme:github lang:js decode:true\">const createUsersTable = () =&gt; {\r\nconst sqlQuery = '\r\n        CREATE TABLE IF NOT EXISTS users (\r\n        id integer PRIMARY KEY,\r\n        name text,\r\n        email text UNIQUE,\r\n        password text)';\r\n\r\n    return database.run(sqlQuery);\r\n}\r\n\r\nconst findUserByEmail = (email, cb) =&gt; {\r\n    return database.get(`SELECT * FROM users WHERE email = ?`, [email], (err, row) =&gt; {\r\n        cb(err, row)\r\n    });\r\n}\r\n\r\nconst createUser = (user, cb) =&gt; {\r\n    return database.run('INSERT INTO users (name, email, password) VALUES (?,?,?)', user, (err) =&gt; {\r\n        cb(err)\r\n    });\r\n}<\/pre>\n<p>After defining these methods to create and work with the database, let&#8217;s create the users table by calling the <span style=\"color: #ff6600;\">createUsersTable()<\/span> right after the definition of the methods:<\/p>\n<pre class=\"theme:github lang:js decode:true\">createUsersTable();<\/pre>\n<p>Stop and run your server\u2014you should see a <span style=\"color: #ff6600;\">my.db<\/span> database file created in the root of your project.<\/p>\n<h3 id=\"configuring-the-jsonwebtoken-bcryptjs-modules\">Configuring the <span style=\"color: #ff6600;\">jsonwebtoken<\/span> &amp; <span style=\"color: #ff6600;\">bcryptjs<\/span> Modules<\/h3>\n<p>Before implementing our authentication flow, we need to setup the <span style=\"color: #ff6600;\">jsonwebtoken<\/span> and <span style=\"color: #ff6600;\">bcryptjs<\/span> modules that are respectively used to create JSON tokens and encrypt passwords before storing them in the database.<\/p>\n<p>First in the <span style=\"color: #ff6600;\">index.js<\/span> file and require <span style=\"color: #ff6600;\">jsonwebtoken<\/span> and <span style=\"color: #ff6600;\">bcryptjs<\/span>:<\/p>\n<pre class=\"theme:github lang:js decode:true\">const jwt = require('jsonwebtoken');\r\nconst bcrypt = require('bcryptjs');<\/pre>\n<p>Also add a secret key that will be used to sign the payloads to create JSON tokens:<\/p>\n<pre class=\"theme:github lang:js decode:true\">const SECRET_KEY = \"secretkey23456\";<\/pre>\n<h3 id=\"implementing-the-register-route\">Implementing the Register Route<\/h3>\n<p>Now, let&#8217;s implement the register route. In the <span style=\"color: #ff6600;\">index.js<\/span> file, add the following code to your register route:<\/p>\n<pre class=\"theme:github lang:js decode:true\">router.post('\/register', (req, res) =&gt; {\r\n\r\n    const name = req.body.name;\r\n    const email = req.body.email;\r\n    const password = bcrypt.hashSync(req.body.password);\r\n\r\n    createUser([name, email, password], (err) =&gt; {\r\n        if (err) return res.status(500).send(\"Server error!\");\r\n        findUserByEmail(email, (err, user) =&gt; {\r\n            if (err) return res.status(500).send('Server error!');\r\n            const expiresIn = 24 * 60 * 60;\r\n            const accessToken = jwt.sign({ id: user.id }, SECRET_KEY, {\r\n                expiresIn: expiresIn\r\n            });\r\n            res.status(200).send({\r\n                \"user\": user, \"access_token\": accessToken, \"expires_in\": expiresIn\r\n            });\r\n        });\r\n    });\r\n});<\/pre>\n<p>We first extract the name, email and password from the request body. Next, we call the <span style=\"color: #ff6600;\">createUser()<\/span> method by passing the extracted credentials for the new user to be created.<\/p>\n<p>In the callback function of the method:<\/p>\n<ul>\n<li>We check if we have an error, in that case we return a <strong>500<\/strong> HTTP response with the Server error! message,<\/li>\n<li>Otherwise, we generate an access token based on the user ID (generated automatically in the database), a secret key and an expires in value (in seconds) using the <span style=\"color: #ff6600;\">sign()<\/span> method of <span style=\"color: #ff6600;\">jsonwebtoken<\/span>,<\/li>\n<li>We finally return a <strong>200<\/strong> response with a body containing the user, access token and the expires in value.<\/li>\n<\/ul>\n<p>Stop and run your server again\u2014If you now send a POST request with the following body:<\/p>\n<pre class=\"theme:github lang:js decode:true\">{\r\n    \"email\": \"test@mail.com\",\r\n    \"name\": \"test\",\r\n    \"password\": \"test001\"\r\n}<\/pre>\n<p>You should get a <strong>200<\/strong> response similar to the following:<\/p>\n<p><img  loading=\"lazy\"  decoding=\"async\"  data-attachment-id=\"4004\"  data-permalink=\"https:\/\/code4developers.com\/node-express-jwt-authentication-using-jsonwebtoken-and-bcryptjs\/express-jwt\/\"  data-orig-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?fit=813%2C307&amp;ssl=1\"  data-orig-size=\"813,307\"  data-comments-opened=\"1\"  data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\"  data-image-title=\"Express-JWT\"  data-image-description=\"\"  data-image-caption=\"\"  data-medium-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?fit=800%2C302&amp;ssl=1\"  data-large-file=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?fit=700%2C264&amp;ssl=1\"  class=\"wp-image-4004 alignnone pk-lazyload\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Express-JWT\"  width=\"592\"  height=\"223\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"auto, (max-width: 592px) 100vw, 592px\"  data-pk-src=\"https:\/\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT-300x113.png\"  data-pk-srcset=\"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=300%2C113&amp;ssl=1 300w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=800%2C302&amp;ssl=1 800w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=700%2C264&amp;ssl=1 700w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=120%2C45&amp;ssl=1 120w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=90%2C34&amp;ssl=1 90w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=320%2C121&amp;ssl=1 320w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=560%2C211&amp;ssl=1 560w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=240%2C91&amp;ssl=1 240w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=180%2C68&amp;ssl=1 180w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=640%2C242&amp;ssl=1 640w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?resize=768%2C290&amp;ssl=1 768w, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/Express-JWT.png?w=813&amp;ssl=1 813w\" ><\/p>\n<p>The response contains the created user information (the password is encrypted using bcrypt), an <span style=\"color: #ff6600;\">access_token<\/span> and <span style=\"color: #ff6600;\">expires_in<\/span> value (one day).<\/p>\n<h3 id=\"implementing-the-login-route\">Implementing the Login Route<\/h3>\n<p>After implementing the logic for registering users let&#8217;s now implement the <span style=\"color: #ff6600;\">login<\/span> route. In the <span style=\"color: #ff6600;\">index.js<\/span> file, add the following code:<\/p>\n<pre class=\"theme:github lang:js decode:true\">router.post('\/login', (req, res) =&gt; {\r\n    const email = req.body.email;\r\n    const password = req.body.password;\r\n    findUserByEmail(email, (err, user) =&gt; {\r\n        if (err) return res.status(500).send('Server error!');\r\n        if (!user) return res.status(404).send('User not found!');\r\n        const result = bcrypt.compareSync(password, user.password);\r\n        if (!result) return res.status(401).send('Password not valid!');\r\n\r\n        const expiresIn = 24 * 60 * 60;\r\n        const accessToken = jwt.sign({ id: user.id }, SECRET_KEY, {\r\n            expiresIn: expiresIn\r\n        });\r\n        res.status(200).send({ \"user\": user, \"access_token\": accessToken, \"expires_in\": expiresIn });\r\n    });\r\n});<\/pre>\n<p>We first extract the login credentials i.e the email and password from the body of the request.<\/p>\n<p>Next, we call the <span style=\"color: #ff6600;\">findUserByEmail()<\/span> method to search for the user with the passed credentials in the SQLite database.<\/p>\n<p>In the callback of the method:<\/p>\n<ul>\n<li>We check of there is an error, in that case we return a <strong>500<\/strong> response,<\/li>\n<li>Next, we check if the user exists in the database and returns a <strong>404<\/strong> response if it doesn&#8217;t,<\/li>\n<li>Next, we check if the passed password matches the database user&#8217;s password using the <span style=\"color: #ff6600;\">bcrypt.compareSync()<\/span> method and returns a <strong>401<\/strong> response if not.<\/li>\n<li>Finally we generate a token using the <span style=\"color: #ff6600;\">sign()<\/span> method of <span style=\"color: #ff6600;\">jsonwebtoken<\/span> and return a <strong>200<\/strong> response with the user, access token and an expiration value.<\/li>\n<\/ul>\n<p>Again stop and run your server\u2014you should be able to login using the previously created user by sending the following data with a POST request to the <span style=\"color: #ff6600;\">\/login<\/span> endpoint:<\/p>\n<pre class=\"theme:github lang:js decode:true\">{\r\n   \"email\": \"test@mail.com\",\r\n   \"password\": \"test001\"\r\n}<\/pre>\n<p>This should return a <strong>200<\/strong> HTTP response with a <span style=\"color: #ff6600;\">user<\/span> object, <span style=\"color: #ff6600;\">access_token<\/span> and <span style=\"color: #ff6600;\">expires_in<\/span> properties.<\/p>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>In this tutorial, we&#8217;ve used Node, Express,<span style=\"color: #ff6600;\"> body-parser<\/span>, <span style=\"color: #ff6600;\">jsonwebtoken<\/span>, <span style=\"color: #ff6600;\">sqlite3<\/span> and <span style=\"color: #ff6600;\">bcrypt<\/span> libraries and packages to create a simple REST server for JWT authentication.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout this tutorial, we&#8217;ll be learning how you can create a JWT authentication server with Node.js and Express.js using some popular libraries like:<\/p>\n","protected":false},"author":7,"featured_media":4008,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[72,260],"tags":[264,266,261,263,80,262,267,265],"powerkit_post_featured":[],"class_list":{"0":"post-4003","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-nodejs","8":"category-jwt-authentication","9":"tag-bcryptjs","10":"tag-body-parser","11":"tag-jsonwebtoken","12":"tag-jwt-authentication","13":"tag-node","14":"tag-node-express","15":"tag-rest","16":"tag-sqlite3"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2019\/01\/nodejs-token-based-authentication.png?fit=1050%2C438&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8NAi4-12z","jetpack-related-posts":[{"id":12847,"url":"https:\/\/code4developers.com\/connect-firebase-database-and-angular-app\/","url_meta":{"origin":4003,"position":0},"title":"Connect Firebase Realtime NoSQL Database with Angular App from Scratch","author":"Arif Khoja","date":"August 30, 2020","format":false,"excerpt":"In this tutorial, We are going to learn\u00a0How to connect Firebase Realtime NoSQL cloud database with Angular app from scratch?. We\u2019ll be using\u00a0AngularFire library for setting up Firebase database in the Angular web application. Firebase is a Google product, It is a real-time NoSQL cloud database that allows you to\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"connect-angular-firebase","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/08\/connect-angular-firebase.jpg?fit=715%2C350&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/08\/connect-angular-firebase.jpg?fit=715%2C350&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/08\/connect-angular-firebase.jpg?fit=715%2C350&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2020\/08\/connect-angular-firebase.jpg?fit=715%2C350&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":2568,"url":"https:\/\/code4developers.com\/angularjs-application-end-to-end-testing-with-protractor-tool-part-1\/","url_meta":{"origin":4003,"position":1},"title":"AngularJS Application End to End Testing with Protractor Tool : Part 1","author":"Sneha Jaiswal","date":"June 8, 2017","format":false,"excerpt":"What is End to End Testing? End to End Testing is used to determine the performance of application as per requirement. For large and complex applications manual testing is not sufficient to verify the correctness of new features, catch bugs and notice regression. To resolve issue of integration between components\u2026","rel":"","context":"In &quot;AngularJs&quot;","block_context":{"text":"AngularJs","link":"https:\/\/code4developers.com\/category\/angularjs\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2586,"url":"https:\/\/code4developers.com\/angular-4-installations\/","url_meta":{"origin":4003,"position":2},"title":"Angular 4 Installations","author":"Nisarg Dave","date":"June 14, 2017","format":false,"excerpt":"Introduction This article demonstrates how to install Angular 4 in your local system and start working with angular\/cli using basic commands. What is Angular 4? Before starting with Angular 4, we need to know about Angular 2. Angular 2 is totally different kind of framework from Angular 1. Angular 1\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1.png?resize=700%2C400 2x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1.png?resize=1050%2C600 3x"},"classes":[]},{"id":2658,"url":"https:\/\/code4developers.com\/angular-4-application-with-visual-studio\/","url_meta":{"origin":4003,"position":3},"title":"Angular 4 in Visual Studio","author":"Nisarg Dave","date":"June 22, 2017","format":false,"excerpt":"Introduction In this article, we will discuss about how to set up and start Angular 4 in visual studio. As many of developers have worked with Microsoft tools and technologies, they preferred visual studio as web development platform. Step 1: Install Node.js and npm The first step is to install\u2026","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/code4developers.com\/category\/angular\/"},"img":{"alt_text":"VS15","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1-2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3165,"url":"https:\/\/code4developers.com\/pug\/","url_meta":{"origin":4003,"position":4},"title":"Pug","author":"Arif Khoja","date":"December 15, 2017","format":false,"excerpt":"Pug is a template language for Javascript that I have grown to enjoy a lot. My impression is that a lot of people use it, if they use a template engine for server side rendering using Node. The big question is if you need it or not in 2017. I\u2026","rel":"","context":"In \"Node\"","block_context":{"text":"Node","link":"https:\/\/code4developers.com\/tag\/node\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/12\/PugJs.png?fit=225%2C225&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":15822,"url":"https:\/\/code4developers.com\/aws-step-functions-tutorial\/","url_meta":{"origin":4003,"position":5},"title":"Getting Started with AWS Step Functions: Orchestration Made Simple","author":"Yatendrasinh Joddha","date":"September 1, 2025","format":false,"excerpt":"Cloud applications are rarely about a single piece of code. Take an e-commerce app for example: you receive an order, validate the payment, check inventory, update the database, and finally send a confirmation email. If you try to put all of this into one Lambda function, the code gets messy,\u2026","rel":"","context":"In &quot;AWS&quot;","block_context":{"text":"AWS","link":"https:\/\/code4developers.com\/category\/aws\/"},"img":{"alt_text":"AWS Step Functions","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2025\/09\/AWS-Step-Function.png?fit=1200%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2025\/09\/AWS-Step-Function.png?fit=1200%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2025\/09\/AWS-Step-Function.png?fit=1200%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2025\/09\/AWS-Step-Function.png?fit=1200%2C800&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2025\/09\/AWS-Step-Function.png?fit=1200%2C800&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/4003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/comments?post=4003"}],"version-history":[{"count":4,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/4003\/revisions"}],"predecessor-version":[{"id":4009,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/4003\/revisions\/4009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media\/4008"}],"wp:attachment":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media?parent=4003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/categories?post=4003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/tags?post=4003"},{"taxonomy":"powerkit_post_featured","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/powerkit_post_featured?post=4003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}