{"id":110927,"date":"2021-07-22T07:00:00","date_gmt":"2021-07-22T04:00:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=110927"},"modified":"2021-07-20T12:48:54","modified_gmt":"2021-07-20T09:48:54","slug":"node-js-jwt-implementation","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html","title":{"rendered":"Node.js JWT Implementation"},"content":{"rendered":"<p>Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access.<\/p>\n<p>The Node.js framework is commonly used to create server-based applications which are further used to show the contents to the users.<\/p>\n<h2>1. Introduction<\/h2>\n<p>JSON web tokens (or the JWT\u2019s) is an Open RFC standard that defines a compact and self-contained way for securely transmitting the information from the server to the client. A json web token is usually divided into 3 parts (header, payload, and signature) separated by a dot symbol i.e. [HEADER].[PAYLOAD].[SIGNATURE].<\/p>\n<ul>\n<li>The header part denotes the crypto operations applied to the token<\/li>\n<li>The payload part denotes the actual data to be transferred using the token. It also contains information such as issuance time, expiration time, and roles (optional)<\/li>\n<li>The signature part denotes the verification that the payload wasn\u2019t changed along the way<\/li>\n<\/ul>\n<h3>1.1 Setting up Node.js<\/h3>\n<p>To set up <strong>Node.js<\/strong> on windows you will need to download the installer from <a href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\" rel=\"noopener\">this<\/a> link. Click on the installer (also include the NPM package manager) for your platform and run the installer to start with the Node.js setup wizard. Follow the wizard steps and click on Finish when it is done. If everything goes well you can navigate to the command prompt to verify if the installation was successful as shown in Fig. 1.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/node-npm-installation-img1-2.jpg\"><img decoding=\"async\" width=\"480\" height=\"91\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/node-npm-installation-img1-2.jpg\" alt=\"nodejs jwt - npm installation\" class=\"wp-image-110928\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/node-npm-installation-img1-2.jpg 480w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/node-npm-installation-img1-2-300x57.jpg 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><figcaption>Fig. 1: Verifying node and npm installation<\/figcaption><\/figure>\n<\/div>\n<h2>2. Node.js JWT implementation<\/h2>\n<p>To set up the application, we will need to navigate to a path where our project will reside. For programming stuff, I am using <a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener\">Visual Studio Code<\/a> as my preferred IDE. You&#8217;re free to choose the IDE of your choice.<\/p>\n<h3>2.1 Setting up dependencies<\/h3>\n<p>Navigate to the project directory and run <code>npm init -y<\/code> to create a <code>package.json<\/code> file. This <a href=\"https:\/\/docs.npmjs.com\/creating-a-package-json-file\" target=\"_blank\" rel=\"noopener\">file<\/a> holds the metadata relevant to the project and is used for managing the project dependencies, script, version, etc. Add the following code to the file wherein we will specify the required dependencies.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\"><em>package.json<\/em><\/span><\/p>\n<pre class=\"brush:json;\">{\n  \"name\": \"jwt\",\n  \"version\": \"1.0.0\",\n  \"description\": \"jwt implementation in nodejs\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"\n  },\n  \"keywords\": [\n    \"nodejs\",\n    \"expressjs\",\n    \"jwt\",\n    \"restapi\"\n  ],\n  \"author\": \"yatbat\",\n  \"license\": \"MIT\",\n  \"dependencies\": {\n    \"express\": \"^4.17.1\",\n    \"jsonwebtoken\": \"^8.5.1\"\n  },\n  \"devDependencies\": {\n    \"nodemon\": \"^2.0.12\"\n  }\n}\n<\/pre>\n<p>To download the dependencies navigate to the directory path containing the file and use the <code>npm install<\/code> command. If everything goes well the dependencies will be loaded inside the <code>node_modules<\/code> folder and you are good to go with the further steps.<\/p>\n<h3>2.2 Setting up Express webserver<\/h3>\n<p>In the root folder add the following content to the <code>index.js<\/code> file. The file will contain the endpoints that will be active once the application is started successfully.<\/p>\n<ul>\n<li>Creating access token endpoint<\/li>\n<li>Unprotected endpoint<\/li>\n<li>The protected endpoint will validate the access token first present in the request header and post validation return the success response. If the access token validation fails forbidden error will be returned to the user<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><em>index.js<\/em><\/span><\/p>\n<pre class=\"brush:js;\">\/\/ importing modules\nconst express = require('express');\nconst jwt = require('jsonwebtoken');\n\nconst app = express();\n\nconst SECRET_KEY = 'MY_SECRET_KEY';\n\n\/\/ non protected endpoint\n\/\/ url - http:\/\/localhost:3001\/api\napp.get('\/api', (req, res) =&gt; {\n    res.status(200).json({ message: 'welcome to api service' });\n});\n\n\/\/ creating access token\n\/\/ url - http:\/\/localhost:3001\/api\/login\napp.post('\/api\/login', (req, res) =&gt; {\n    \/\/ todo - add request body validation\n    \/\/ throw 400 bad request if username or password is null\n    \/\/ throw 401 unauthorized if username or password is incorrect\n\n    \/\/ creating payload\n    let nowInSeconds = new Date().getTime() \/ 1000;\n    let payload = {\n        aud: 'e78dc489-e37e-4aa3-9247-cd6b214da3e6',\n        iss: 'node',\n        sub: 'jcg',\n        iat: nowInSeconds\n    };\n    \/\/ creating access-token\n    const accessToken = jwt.sign(payload, SECRET_KEY, { algorithm: 'HS256', expiresIn: '1h' });\n    res.status(201).json({ token: accessToken });\n});\n\n\/\/ protected endpoint\n\/\/ will verify the access token first\n\/\/ url - http:\/\/localhost:3001\/api\/protected\n\/\/ note - add the authorization header in the request otherwise you will get 403 error\napp.get('\/api\/protected', ensureToken, (req, res) =&gt; {\n    \/\/ verifying the jwt token\n    jwt.verify(req.token, SECRET_KEY, { algorithm: 'HS256' }, (err, data) =&gt; {\n        if (err) {\n            \/\/ console.log(err);\n            res.status(403).json({ message: 'Forbidden' });\n        }\n        else {\n            \/\/ console.log(data);\n            res.status(200).json({ message: 'welcome to protected api service' });\n        }\n    });\n});\n\n\/\/ util method\nfunction ensureToken(req, res, next) {\n    const bearerHeader = req.headers['authorization'];\n    \/\/ console.log('Bearer header received = ' + bearerHeader)\n    if (typeof bearerHeader !== 'undefined') {\n        const bearer = bearerHeader.split(' ');\n        const bearerToken = bearer[1];\n        req.token = bearerToken;\n        next();\n    } else {\n        res.status(403).json({ message: 'Forbidden' });\n    }\n}\n\n\/\/ start app\nconst PORT = process.env.port || 3001;\napp.listen(PORT, () =&gt; {\n    console.log(`Application listening on ${PORT}`);\n});\n<\/pre>\n<h2>3. Run the Application<\/h2>\n<p>To run the application navigate to the project directory and enter the following command as shown in Fig. 2. If everything goes well the application will be started successfully on port number <code>3001<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/code-run-2.jpg\"><img decoding=\"async\" width=\"481\" height=\"176\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/code-run-2.jpg\" alt=\"nodejs jwt - starting the app\" class=\"wp-image-110929\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/code-run-2.jpg 481w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/code-run-2-300x110.jpg 300w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><\/a><figcaption>Fig. 2: Starting the application<\/figcaption><\/figure>\n<\/div>\n<h2>4. Demo<\/h2>\n<p>You are free to use <a href=\"https:\/\/www.getpostman.com\/\" target=\"_blank\" rel=\"noopener\">postman<\/a> or any other tool of your choice to make the HTTP request to the application endpoints.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">\/\/ Non protected endpoint\n\/\/ HTTP GET\nhttp:\/\/localhost:3001\/api\n\n\/\/ Creating access token endpoint\n\/\/ HTTP POST\nhttp:\/\/localhost:3001\/api\/login\n\n\/\/ Protected endpoint\n\/\/ HTTP GET\nhttp:\/\/localhost:3001\/api\/protected\n<\/pre>\n<p>That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!<\/p>\n<h2>5. Summary<\/h2>\n<p>In this tutorial, we learned how to create a Node.js JWT (JSON Web Token) using the <code>jsonwebtoken<\/code> module and verifying the access token while calling the protected endpoint. You can download the source code and the postman collection from the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>6. Download the Project<\/h2>\n<p>This was a tutorial on how to implement a JWT (JSON web token) in a node.js application.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/07\/Nodejs-JWT-implementation.zip\"><strong>Node.js JWT Implementation<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js framework is commonly used to create server-based applications which are further used to show the contents to the users. 1. Introduction JSON web tokens (or the JWT\u2019s) is an Open RFC standard &hellip;<\/p>\n","protected":false},"author":26931,"featured_media":20900,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1879],"tags":[991,1289,1712,741],"class_list":["post-110927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-express-js","tag-jwt","tag-jwt-authentication","tag-node-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.js JWT Implementation - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js JWT Implementation - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-22T04:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin Batra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin Batra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html\"},\"author\":{\"name\":\"Yatin Batra\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\"},\"headline\":\"Node.js JWT Implementation\",\"datePublished\":\"2021-07-22T04:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html\"},\"wordCount\":615,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"keywords\":[\"Express.js\",\"JWT\",\"JWT Authentication\",\"Node.js\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html\",\"name\":\"Node.js JWT Implementation - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"datePublished\":\"2021-07-22T04:00:00+00:00\",\"description\":\"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/nodejs-jwt-implementation.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Development\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Node.js JWT Implementation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\",\"name\":\"Yatin Batra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"caption\":\"Yatin Batra\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\\\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yatin-batra\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js JWT Implementation - Java Code Geeks","description":"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html","og_locale":"en_US","og_type":"article","og_title":"Node.js JWT Implementation - Java Code Geeks","og_description":"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js","og_url":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-07-22T04:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","type":"image\/jpeg"}],"author":"Yatin Batra","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin Batra","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html"},"author":{"name":"Yatin Batra","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d"},"headline":"Node.js JWT Implementation","datePublished":"2021-07-22T04:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html"},"wordCount":615,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","keywords":["Express.js","JWT","JWT Authentication","Node.js"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html","url":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html","name":"Node.js JWT Implementation - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","datePublished":"2021-07-22T04:00:00+00:00","description":"Hello. In this tutorial, we will implement a Node.js JWT (JSON Web Token) to protect the application endpoints from unauthorized access. The Node.js","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/nodejs-jwt-implementation.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Development","item":"https:\/\/www.javacodegeeks.com\/category\/web-development"},{"@type":"ListItem","position":3,"name":"JavaScript","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript"},{"@type":"ListItem","position":4,"name":"Node.js JWT Implementation"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d","name":"Yatin Batra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","caption":"Yatin Batra"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/www.javacodegeeks.com\/author\/yatin-batra"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/110927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26931"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=110927"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/110927\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/20900"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=110927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=110927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=110927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}