{"id":1418,"date":"2026-07-10T18:18:17","date_gmt":"2026-07-10T11:18:17","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/10\/how-to-validate-json-web-tokens-jwt-in-node-js\/"},"modified":"2026-07-10T18:18:17","modified_gmt":"2026-07-10T11:18:17","slug":"how-to-validate-json-web-tokens-jwt-in-node-js","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/10\/how-to-validate-json-web-tokens-jwt-in-node-js\/","title":{"rendered":"How to Validate JSON Web Tokens (JWT) in Node.js"},"content":{"rendered":"<h1>How to Validate JSON Web Tokens (JWT) in Node.js<\/h1>\n<p>JSON Web Tokens (JWT) are widely used for authentication and authorization in Node.js applications. Validation ensures the token hasn&#8217;t been tampered with, is not expired, and was issued by a trusted source. This tutorial walks you through the essential steps to validate JWTs using the popular `jsonwebtoken` library.<\/p>\n<p>Before proceeding, make sure you have Node.js installed and a project initialized. You\u2019ll also need the secret key (or public key for RS256) that was used to sign the token. If you don\u2019t have a token, you can create one for testing using <code>jwt.sign()<\/code>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20validate%20JSON%20Web%20Tokens%20in%20Node\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Install the Required Package<\/h2>\n<p>Add the `jsonwebtoken` library to your project:<\/p>\n<pre><code>npm install jsonwebtoken<\/code><\/pre>\n<p>This module provides `jwt.verify()` which handles signature verification and expiration checking in one call.<\/p>\n<h2>2. Basic Token Validation<\/h2>\n<p>Import the library and call `jwt.verify()` with the token and your secret key:<\/p>\n<pre><code>const jwt = require('jsonwebtoken');\n\nconst token = \"your.jwt.token.here\";\nconst secret = \"your-secret-key\";\n\ntry {\n  const decoded = jwt.verify(token, secret);\n  console.log('Valid token:', decoded);\n} catch (err) {\n  console.error('Invalid token:', err.message);\n}<\/code><\/pre>\n<p>The `decoded` object contains the payload (e.g., `userId`, `role`) plus `iat` and `exp` fields if present.<\/p>\n<h2>3. Handle Common Validation Errors<\/h2>\n<p>`jwt.verify()` throws specific errors you should catch:<\/p>\n<ul>\n<li><strong>TokenExpiredError<\/strong> \u2013 the token\u2019s expiration time has passed.<\/li>\n<li><strong>JsonWebTokenError<\/strong> \u2013 signature mismatch, malformed token, or other issues.<\/li>\n<li><strong>NotBeforeError<\/strong> \u2013 the `nbf` claim hasn\u2019t been reached yet.<\/li>\n<\/ul>\n<p>Always wrap the call in a try\/catch block to deliver appropriate client responses (e.g., 401 Unauthorized).<\/p>\n<h2>4. Using Async\/Await with Promises<\/h2>\n<p>For cleaner code, wrap `jwt.verify()` in a Promise:<\/p>\n<pre><code>function verifyToken(token) {\n  return new Promise((resolve, reject) => {\n    jwt.verify(token, secret, (err, decoded) => {\n      if (err) reject(err);\n      else resolve(decoded);\n    });\n  });\n}\n\n\/\/ Usage\n(async () => {\n  try {\n    const decoded = await verifyToken(token);\n    console.log('Token is valid');\n  } catch (err) {\n    console.error('Validation failed');\n  }\n})();<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Validating JWTs in Node.js is straightforward with the `jsonwebtoken` library. Always check for errors, handle expiration gracefully, and never trust a token without verification. For production, store secrets in environment variables and consider rotating keys regularly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Validate JSON Web Tokens (JWT) in Node.js JSON Web Tokens (JWT) are widely used for authentication and authorization in Node.js applications. Validation ensures the token hasn&#8217;t been tampered with, is not expired, and was issued by a trusted source. This tutorial walks you through the essential steps to validate JWTs using the popular &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-1418","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1418"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1418\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}