{"id":1456,"date":"2025-04-22T13:48:01","date_gmt":"2025-04-22T13:48:01","guid":{"rendered":"https:\/\/www.devcentrehouse.eu\/blogs\/?p=1456"},"modified":"2025-08-14T14:41:38","modified_gmt":"2025-08-14T14:41:38","slug":"authentication-in-nestjs-using-jwt","status":"publish","type":"post","link":"https:\/\/www.devcentrehouse.eu\/blogs\/authentication-in-nestjs-using-jwt\/","title":{"rendered":"Authentication and Authorization in NestJS Using JWT"},"content":{"rendered":"<!-- VideographyWP Plugin Message: Automatic video embedding prevented by plugin options. -->\n\n<p>Security is a crucial aspect of web applications. In&nbsp;<strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/NestJS\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">NestJS<\/a><\/strong>, handling authentication and authorization efficiently is essential to ensure only authenticated users can access protected resources. One of the most common and secure methods to achieve this is using&nbsp;<strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/JSON_Web_Token\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">JSON Web Tokens <\/a>(JWT)<\/strong>.<\/p>\n\n\n\n<p>In this guide, we&#8217;ll explore how to implement authentication and authorization in a&nbsp;<strong>NestJS<\/strong>&nbsp;application using&nbsp;<strong>JWT<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before starting, make sure you have&nbsp;<strong>Node.js<\/strong>&nbsp;installed and a&nbsp;<strong>NestJS<\/strong>&nbsp;project set up. If not, you can create a new NestJS project using:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"98\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.28-1024x98.png\" alt=\"\" class=\"wp-image-1458\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.28-1024x98.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.28-300x29.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.28-768x74.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.28.png 1376w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Dependencies<\/h2>\n\n\n\n<p>To implement JWT authentication, install the required dependencies:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"75\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.34-1024x75.png\" alt=\"\" class=\"wp-image-1459\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.34-1024x75.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.34-300x22.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.34-768x56.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.34.png 1370w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up Authentication<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the User Entity<\/h3>\n\n\n\n<p>Create a&nbsp;<code>user.entity.ts<\/code>&nbsp;file inside a&nbsp;<code>users<\/code>&nbsp;module:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"317\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.39-1024x317.png\" alt=\"\" class=\"wp-image-1460\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.39-1024x317.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.39-300x93.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.39-768x238.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.41.39.png 1384w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Auth Service<\/h3>\n\n\n\n<p>Generate an&nbsp;<strong>auth module<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"99\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.40-1024x99.png\" alt=\"\" class=\"wp-image-1461\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.40-1024x99.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.40-300x29.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.40-768x74.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.40.png 1392w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Modify&nbsp;<code>auth.service.ts<\/code>&nbsp;to handle user authentication and JWT generation:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"635\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.46-1024x635.png\" alt=\"\" class=\"wp-image-1462\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.46-1024x635.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.46-300x186.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.46-768x476.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.46.png 1390w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring JWT Strategy<\/h3>\n\n\n\n<p>Create&nbsp;<code>jwt.strategy.ts<\/code>&nbsp;inside the&nbsp;<code>auth<\/code>&nbsp;module:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"436\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.52-1024x436.png\" alt=\"\" class=\"wp-image-1463\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.52-1024x436.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.52-300x128.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.52-768x327.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.42.52.png 1386w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Updating the Auth Module<\/h3>\n\n\n\n<p>Modify&nbsp;<code>auth.module.ts<\/code>&nbsp;to import necessary services and modules:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.02-1024x462.png\" alt=\"\" class=\"wp-image-1464\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.02-1024x462.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.02-300x135.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.02-768x346.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.02.png 1388w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Auth Controller<\/h3>\n\n\n\n<p>Modify&nbsp;<code>auth.controller.ts<\/code>&nbsp;to handle authentication routes:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.10-1024x462.png\" alt=\"\" class=\"wp-image-1467\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.10-1024x462.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.10-300x135.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.10-768x346.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.10.png 1388w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the JWT Auth Guard<\/h3>\n\n\n\n<p>Create&nbsp;<code>jwt-auth.guard.ts<\/code>&nbsp;inside the&nbsp;<code>auth<\/code>&nbsp;module:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"234\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.17-1024x234.png\" alt=\"\" class=\"wp-image-1468\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.17-1024x234.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.17-300x69.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.17-768x176.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.43.17.png 1380w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Authentication<\/h2>\n\n\n\n<p>Start your NestJS application:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"62\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.44.57-1024x62.png\" alt=\"\" class=\"wp-image-1469\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.44.57-1024x62.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.44.57-300x18.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.44.57-768x47.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.44.57.png 1350w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Login<\/h3>\n\n\n\n<p>Make a&nbsp;<strong>POST<\/strong>&nbsp;request to&nbsp;<code>http:\/\/localhost:3000\/auth\/login<\/code>&nbsp;with:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"122\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.02-1024x122.png\" alt=\"\" class=\"wp-image-1470\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.02-1024x122.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.02-300x36.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.02-768x91.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.02.png 1378w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It will return:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"97\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.10-1024x97.png\" alt=\"\" class=\"wp-image-1471\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.10-1024x97.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.10-300x28.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.10-768x73.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.10.png 1390w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. Access Protected Route<\/h3>\n\n\n\n<p>Make a&nbsp;<strong>POST<\/strong>&nbsp;request to&nbsp;<code>http:\/\/localhost:3000\/auth\/profile<\/code>&nbsp;with the token in the headers:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"53\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.15-1024x53.png\" alt=\"\" class=\"wp-image-1472\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.15-1024x53.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.15-300x16.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.15-768x40.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.15.png 1392w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You will receive:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"124\" src=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.21-1024x124.png\" alt=\"\" class=\"wp-image-1473\" srcset=\"https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.21-1024x124.png 1024w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.21-300x36.png 300w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.21-768x93.png 768w, https:\/\/www.devcentrehouse.eu\/blogs\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-14.45.21.png 1358w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Focus on Dev Centre House Ireland as a Partner<\/h2>\n\n\n\n<p>This successful implementation of JWT-based authentication and authorization in NestJS reflects the standards of excellence Dev Centre House Ireland promotes in their Node.js back-end development. As a partner, their expertise ensures that your application benefits from secure and efficient architecture. You have established a foundation for protected resources, and future enhancements like refresh tokens, RBAC, and comprehensive user management can be readily integrated, drawing on the proven Node.js proficiency of <a href=\"https:\/\/www.devcentrehouse.eu\/en\/technologies\/back-end\/nodejs\">Dev Centre House Ireland<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<p><strong>Question: What is JWT authentication, and why use it in NestJS?<\/strong><br><strong>Answer:<\/strong> JWT (JSON Web Token) authentication enables secure, stateless user sessions in APIs. In NestJS, it simplifies token-based authentication without storing session data server-side.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: How does NestJS implement JWT authentication?<\/strong><br><strong>Answer:<\/strong> NestJS uses the <code>@nestjs\/jwt<\/code> and <code>passport-jwt<\/code> packages along with guards, strategies, and modules to validate tokens and protect routes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: What is a JWT payload,<\/strong> and what should it contain?<br>Answer: The payload is the data inside the token, usually including user ID, roles, and expiry. Keep it minimal to improve performance and reduce risks if intercepted.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: How do you secure JWT secrets in NestJS?<\/strong><br><strong>Answer:<\/strong> Store secrets in environment variables or vaults\u2014not in code\u2014then reference them via <code>ConfigService<\/code> to configure JWT signing and verification.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: What is the TokenService in NestJS JWT authentication?<\/strong><br><strong>Answer:<\/strong> A custom service that generates tokens with <code>JwtService<\/code>, setting payload, expiry, and signing options to produce access and refresh tokens.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: How can you invalidate JWT tokens before expiration?<\/strong><br><strong>Answer:<\/strong> Use refresh token strategy or maintain a token blacklist (e.g., in Redis) to prevent further use of compromised or logged-out tokens.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: What are guards and strategies in NestJS auth?<\/strong><br><strong>Answer:<\/strong> A strategy defines how to validate incoming tokens. A guard uses that strategy to protect routes and enforce authentication before controller execution.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: How do you implement JWT refresh tokens in NestJS?<\/strong><br><strong>Answer:<\/strong> Issue short-lived access tokens and long-lived refresh tokens. Use a dedicated refresh route and guard to generate new access tokens when valid.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: What should I do when a JWT expires?<\/strong><br><strong>Answer:<\/strong> Catch the <code>UnauthorizedException<\/code>, then prompt the client to refresh the access token via the refresh endpoint, avoiding full re-authentication.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Question: How can I test JWT authentication in NestJS?<\/strong><br><strong>Answer:<\/strong> Use tools like Postman to simulate login, attach tokens in the Authorization header, and validate access to protected endpoints.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<noscript class=\"ninja-forms-noscript-message\">\n\tNotice: JavaScript is required for this content.<\/noscript>\n<div id=\"nf-form-1-cont\" class=\"nf-form-cont\" aria-live=\"polite\" aria-labelledby=\"nf-form-title-1\" aria-describedby=\"nf-form-errors-1\" role=\"form\">\n\n    <div class=\"nf-loading-spinner\"><\/div>\n\n<\/div>\n        <!-- That data is being printed as a workaround to page builders reordering the order of the scripts loaded-->\n        <script>var formDisplay=1;var nfForms=nfForms||[];var form=[];form.id='1';form.settings={\"objectType\":\"Form Setting\",\"editActive\":true,\"title\":\"Contact Dev Centre House Ireland\",\"key\":\"\",\"created_at\":\"2024-10-01 08:18:59\",\"default_label_pos\":\"above\",\"conditions\":[],\"show_title\":\"1\",\"clear_complete\":\"1\",\"hide_complete\":\"1\",\"wrapper_class\":\"\",\"element_class\":\"\",\"add_submit\":\"1\",\"logged_in\":\"\",\"not_logged_in_msg\":\"\",\"sub_limit_number\":\"\",\"sub_limit_msg\":\"\",\"calculations\":[],\"formContentData\":[\"name\",\"email\",\"message\",\"submit\"],\"container_styles_background-color\":\"\",\"container_styles_border\":\"\",\"container_styles_border-style\":\"\",\"container_styles_border-color\":\"\",\"container_styles_color\":\"\",\"container_styles_height\":\"\",\"container_styles_width\":\"\",\"container_styles_font-size\":\"\",\"container_styles_margin\":\"\",\"container_styles_padding\":\"\",\"container_styles_display\":\"\",\"container_styles_float\":\"\",\"container_styles_show_advanced_css\":\"0\",\"container_styles_advanced\":\"\",\"title_styles_background-color\":\"\",\"title_styles_border\":\"\",\"title_styles_border-style\":\"\",\"title_styles_border-color\":\"\",\"title_styles_color\":\"\",\"title_styles_height\":\"\",\"title_styles_width\":\"\",\"title_styles_font-size\":\"\",\"title_styles_margin\":\"\",\"title_styles_padding\":\"\",\"title_styles_display\":\"\",\"title_styles_float\":\"\",\"title_styles_show_advanced_css\":\"0\",\"title_styles_advanced\":\"\",\"row_styles_background-color\":\"\",\"row_styles_border\":\"\",\"row_styles_border-style\":\"\",\"row_styles_border-color\":\"\",\"row_styles_color\":\"\",\"row_styles_height\":\"\",\"row_styles_width\":\"\",\"row_styles_font-size\":\"\",\"row_styles_margin\":\"\",\"row_styles_padding\":\"\",\"row_styles_display\":\"\",\"row_styles_show_advanced_css\":\"0\",\"row_styles_advanced\":\"\",\"row-odd_styles_background-color\":\"\",\"row-odd_styles_border\":\"\",\"row-odd_styles_border-style\":\"\",\"row-odd_styles_border-color\":\"\",\"row-odd_styles_color\":\"\",\"row-odd_styles_height\":\"\",\"row-odd_styles_width\":\"\",\"row-odd_styles_font-size\":\"\",\"row-odd_styles_margin\":\"\",\"row-odd_styles_padding\":\"\",\"row-odd_styles_display\":\"\",\"row-odd_styles_show_advanced_css\":\"0\",\"row-odd_styles_advanced\":\"\",\"success-msg_styles_background-color\":\"\",\"success-msg_styles_border\":\"\",\"success-msg_styles_border-style\":\"\",\"success-msg_styles_border-color\":\"\",\"success-msg_styles_color\":\"\",\"success-msg_styles_height\":\"\",\"success-msg_styles_width\":\"\",\"success-msg_styles_font-size\":\"\",\"success-msg_styles_margin\":\"\",\"success-msg_styles_padding\":\"\",\"success-msg_styles_display\":\"\",\"success-msg_styles_show_advanced_css\":\"0\",\"success-msg_styles_advanced\":\"\",\"error_msg_styles_background-color\":\"\",\"error_msg_styles_border\":\"\",\"error_msg_styles_border-style\":\"\",\"error_msg_styles_border-color\":\"\",\"error_msg_styles_color\":\"\",\"error_msg_styles_height\":\"\",\"error_msg_styles_width\":\"\",\"error_msg_styles_font-size\":\"\",\"error_msg_styles_margin\":\"\",\"error_msg_styles_padding\":\"\",\"error_msg_styles_display\":\"\",\"error_msg_styles_show_advanced_css\":\"0\",\"error_msg_styles_advanced\":\"\",\"allow_public_link\":0,\"embed_form\":\"\",\"form_title_heading_level\":\"3\",\"currency\":\"\",\"unique_field_error\":\"A form with this value has already been submitted.\",\"changeEmailErrorMsg\":\"Please enter a valid email address!\",\"changeDateErrorMsg\":\"Please enter a valid date!\",\"confirmFieldErrorMsg\":\"These fields must match!\",\"fieldNumberNumMinError\":\"Number Min Error\",\"fieldNumberNumMaxError\":\"Number Max Error\",\"fieldNumberIncrementBy\":\"Please increment by \",\"formErrorsCorrectErrors\":\"Please correct errors before submitting this form.\",\"validateRequiredField\":\"This is a required field.\",\"honeypotHoneypotError\":\"Honeypot Error\",\"fieldsMarkedRequired\":\"Fields marked with an <span class=\\\"ninja-forms-req-symbol\\\">*<\\\/span> are required\",\"objectDomain\":\"display\",\"drawerDisabled\":false,\"ninjaForms\":\"Ninja Forms\",\"fieldTextareaRTEInsertLink\":\"Insert Link\",\"fieldTextareaRTEInsertMedia\":\"Insert Media\",\"fieldTextareaRTESelectAFile\":\"Select a file\",\"formHoneypot\":\"If you are a human seeing this field, please leave it empty.\",\"fileUploadOldCodeFileUploadInProgress\":\"File Upload in Progress.\",\"fileUploadOldCodeFileUpload\":\"FILE UPLOAD\",\"currencySymbol\":\"&#36;\",\"thousands_sep\":\",\",\"decimal_point\":\".\",\"siteLocale\":\"en_US\",\"dateFormat\":\"m\\\/d\\\/Y\",\"startOfWeek\":\"1\",\"of\":\"of\",\"previousMonth\":\"Previous Month\",\"nextMonth\":\"Next Month\",\"months\":[\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"],\"monthsShort\":[\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"],\"weekdays\":[\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"],\"weekdaysShort\":[\"Sun\",\"Mon\",\"Tue\",\"Wed\",\"Thu\",\"Fri\",\"Sat\"],\"weekdaysMin\":[\"Su\",\"Mo\",\"Tu\",\"We\",\"Th\",\"Fr\",\"Sa\"],\"recaptchaConsentMissing\":\"reCaptcha validation couldn&#039;t load.\",\"recaptchaMissingCookie\":\"reCaptcha v3 validation couldn&#039;t load the cookie needed to submit the form.\",\"recaptchaConsentEvent\":\"Accept reCaptcha cookies before sending the form.\",\"currency_symbol\":\"\",\"beforeForm\":\"\",\"beforeFields\":\"\",\"afterFields\":\"\",\"afterForm\":\"\"};form.fields=[{\"objectType\":\"Field\",\"objectDomain\":\"fields\",\"editActive\":false,\"order\":1,\"idAttribute\":\"id\",\"label\":\"Name\",\"key\":\"name\",\"type\":\"textbox\",\"created_at\":\"2024-10-01 08:19:00\",\"label_pos\":\"above\",\"required\":1,\"placeholder\":\"\",\"default\":\"\",\"wrapper_class\":\"\",\"element_class\":\"\",\"container_class\":\"\",\"input_limit\":\"\",\"input_limit_type\":\"characters\",\"input_limit_msg\":\"Character(s) left\",\"manual_key\":\"\",\"disable_input\":\"\",\"admin_label\":\"\",\"help_text\":\"\",\"desc_text\":\"\",\"disable_browser_autocomplete\":\"\",\"mask\":\"\",\"custom_mask\":\"\",\"wrap_styles_background-color\":\"\",\"wrap_styles_border\":\"\",\"wrap_styles_border-style\":\"\",\"wrap_styles_border-color\":\"\",\"wrap_styles_color\":\"\",\"wrap_styles_height\":\"\",\"wrap_styles_width\":\"\",\"wrap_styles_font-size\":\"\",\"wrap_styles_margin\":\"\",\"wrap_styles_padding\":\"\",\"wrap_styles_display\":\"\",\"wrap_styles_float\":\"\",\"wrap_styles_show_advanced_css\":0,\"wrap_styles_advanced\":\"\",\"label_styles_background-color\":\"\",\"label_styles_border\":\"\",\"label_styles_border-style\":\"\",\"label_styles_border-color\":\"\",\"label_styles_color\":\"\",\"label_styles_height\":\"\",\"label_styles_width\":\"\",\"label_styles_font-size\":\"\",\"label_styles_margin\":\"\",\"label_styles_padding\":\"\",\"label_styles_display\":\"\",\"label_styles_float\":\"\",\"label_styles_show_advanced_css\":0,\"label_styles_advanced\":\"\",\"element_styles_background-color\":\"\",\"element_styles_border\":\"\",\"element_styles_border-style\":\"\",\"element_styles_border-color\":\"\",\"element_styles_color\":\"\",\"element_styles_height\":\"\",\"element_styles_width\":\"\",\"element_styles_font-size\":\"\",\"element_styles_margin\":\"\",\"element_styles_padding\":\"\",\"element_styles_display\":\"\",\"element_styles_float\":\"\",\"element_styles_show_advanced_css\":0,\"element_styles_advanced\":\"\",\"cellcid\":\"c3277\",\"custom_name_attribute\":\"\",\"personally_identifiable\":\"\",\"value\":\"\",\"id\":1,\"beforeField\":\"\",\"afterField\":\"\",\"parentType\":\"textbox\",\"element_templates\":[\"textbox\",\"input\"],\"old_classname\":\"\",\"wrap_template\":\"wrap\"},{\"objectType\":\"Field\",\"objectDomain\":\"fields\",\"editActive\":false,\"order\":2,\"idAttribute\":\"id\",\"label\":\"Email\",\"key\":\"email\",\"type\":\"email\",\"created_at\":\"2024-10-01 08:19:01\",\"label_pos\":\"above\",\"required\":1,\"placeholder\":\"\",\"default\":\"\",\"wrapper_class\":\"\",\"element_class\":\"\",\"container_class\":\"\",\"admin_label\":\"\",\"help_text\":\"\",\"desc_text\":\"\",\"wrap_styles_background-color\":\"\",\"wrap_styles_border\":\"\",\"wrap_styles_border-style\":\"\",\"wrap_styles_border-color\":\"\",\"wrap_styles_color\":\"\",\"wrap_styles_height\":\"\",\"wrap_styles_width\":\"\",\"wrap_styles_font-size\":\"\",\"wrap_styles_margin\":\"\",\"wrap_styles_padding\":\"\",\"wrap_styles_display\":\"\",\"wrap_styles_float\":\"\",\"wrap_styles_show_advanced_css\":0,\"wrap_styles_advanced\":\"\",\"label_styles_background-color\":\"\",\"label_styles_border\":\"\",\"label_styles_border-style\":\"\",\"label_styles_border-color\":\"\",\"label_styles_color\":\"\",\"label_styles_height\":\"\",\"label_styles_width\":\"\",\"label_styles_font-size\":\"\",\"label_styles_margin\":\"\",\"label_styles_padding\":\"\",\"label_styles_display\":\"\",\"label_styles_float\":\"\",\"label_styles_show_advanced_css\":0,\"label_styles_advanced\":\"\",\"element_styles_background-color\":\"\",\"element_styles_border\":\"\",\"element_styles_border-style\":\"\",\"element_styles_border-color\":\"\",\"element_styles_color\":\"\",\"element_styles_height\":\"\",\"element_styles_width\":\"\",\"element_styles_font-size\":\"\",\"element_styles_margin\":\"\",\"element_styles_padding\":\"\",\"element_styles_display\":\"\",\"element_styles_float\":\"\",\"element_styles_show_advanced_css\":0,\"element_styles_advanced\":\"\",\"cellcid\":\"c3281\",\"custom_name_attribute\":\"email\",\"personally_identifiable\":1,\"value\":\"\",\"id\":2,\"beforeField\":\"\",\"afterField\":\"\",\"parentType\":\"email\",\"element_templates\":[\"email\",\"input\"],\"old_classname\":\"\",\"wrap_template\":\"wrap\"},{\"objectType\":\"Field\",\"objectDomain\":\"fields\",\"editActive\":false,\"order\":3,\"idAttribute\":\"id\",\"label\":\"Message\",\"key\":\"message\",\"type\":\"textarea\",\"created_at\":\"2024-10-01 08:19:01\",\"label_pos\":\"above\",\"required\":1,\"placeholder\":\"\",\"default\":\"\",\"wrapper_class\":\"\",\"element_class\":\"\",\"container_class\":\"\",\"input_limit\":\"\",\"input_limit_type\":\"characters\",\"input_limit_msg\":\"Character(s) left\",\"manual_key\":\"\",\"disable_input\":\"\",\"admin_label\":\"\",\"help_text\":\"\",\"desc_text\":\"\",\"disable_browser_autocomplete\":\"\",\"textarea_rte\":\"\",\"disable_rte_mobile\":\"\",\"textarea_media\":\"\",\"wrap_styles_background-color\":\"\",\"wrap_styles_border\":\"\",\"wrap_styles_border-style\":\"\",\"wrap_styles_border-color\":\"\",\"wrap_styles_color\":\"\",\"wrap_styles_height\":\"\",\"wrap_styles_width\":\"\",\"wrap_styles_font-size\":\"\",\"wrap_styles_margin\":\"\",\"wrap_styles_padding\":\"\",\"wrap_styles_display\":\"\",\"wrap_styles_float\":\"\",\"wrap_styles_show_advanced_css\":0,\"wrap_styles_advanced\":\"\",\"label_styles_background-color\":\"\",\"label_styles_border\":\"\",\"label_styles_border-style\":\"\",\"label_styles_border-color\":\"\",\"label_styles_color\":\"\",\"label_styles_height\":\"\",\"label_styles_width\":\"\",\"label_styles_font-size\":\"\",\"label_styles_margin\":\"\",\"label_styles_padding\":\"\",\"label_styles_display\":\"\",\"label_styles_float\":\"\",\"label_styles_show_advanced_css\":0,\"label_styles_advanced\":\"\",\"element_styles_background-color\":\"\",\"element_styles_border\":\"\",\"element_styles_border-style\":\"\",\"element_styles_border-color\":\"\",\"element_styles_color\":\"\",\"element_styles_height\":\"\",\"element_styles_width\":\"\",\"element_styles_font-size\":\"\",\"element_styles_margin\":\"\",\"element_styles_padding\":\"\",\"element_styles_display\":\"\",\"element_styles_float\":\"\",\"element_styles_show_advanced_css\":0,\"element_styles_advanced\":\"\",\"cellcid\":\"c3284\",\"value\":\"\",\"id\":3,\"beforeField\":\"\",\"afterField\":\"\",\"parentType\":\"textarea\",\"element_templates\":[\"textarea\",\"input\"],\"old_classname\":\"\",\"wrap_template\":\"wrap\"},{\"objectType\":\"Field\",\"objectDomain\":\"fields\",\"editActive\":false,\"order\":4,\"idAttribute\":\"id\",\"label\":\"Submit\",\"key\":\"submit\",\"type\":\"submit\",\"created_at\":\"2024-10-01 08:19:02\",\"processing_label\":\"Processing\",\"container_class\":\"\",\"element_class\":\"\",\"wrap_styles_background-color\":\"\",\"wrap_styles_border\":\"\",\"wrap_styles_border-style\":\"\",\"wrap_styles_border-color\":\"\",\"wrap_styles_color\":\"\",\"wrap_styles_height\":\"\",\"wrap_styles_width\":\"\",\"wrap_styles_font-size\":\"\",\"wrap_styles_margin\":\"\",\"wrap_styles_padding\":\"\",\"wrap_styles_display\":\"\",\"wrap_styles_float\":\"\",\"wrap_styles_show_advanced_css\":0,\"wrap_styles_advanced\":\"\",\"label_styles_background-color\":\"\",\"label_styles_border\":\"\",\"label_styles_border-style\":\"\",\"label_styles_border-color\":\"\",\"label_styles_color\":\"\",\"label_styles_height\":\"\",\"label_styles_width\":\"\",\"label_styles_font-size\":\"\",\"label_styles_margin\":\"\",\"label_styles_padding\":\"\",\"label_styles_display\":\"\",\"label_styles_float\":\"\",\"label_styles_show_advanced_css\":0,\"label_styles_advanced\":\"\",\"element_styles_background-color\":\"\",\"element_styles_border\":\"\",\"element_styles_border-style\":\"\",\"element_styles_border-color\":\"\",\"element_styles_color\":\"\",\"element_styles_height\":\"\",\"element_styles_width\":\"\",\"element_styles_font-size\":\"\",\"element_styles_margin\":\"\",\"element_styles_padding\":\"\",\"element_styles_display\":\"\",\"element_styles_float\":\"\",\"element_styles_show_advanced_css\":0,\"element_styles_advanced\":\"\",\"submit_element_hover_styles_background-color\":\"\",\"submit_element_hover_styles_border\":\"\",\"submit_element_hover_styles_border-style\":\"\",\"submit_element_hover_styles_border-color\":\"\",\"submit_element_hover_styles_color\":\"\",\"submit_element_hover_styles_height\":\"\",\"submit_element_hover_styles_width\":\"\",\"submit_element_hover_styles_font-size\":\"\",\"submit_element_hover_styles_margin\":\"\",\"submit_element_hover_styles_padding\":\"\",\"submit_element_hover_styles_display\":\"\",\"submit_element_hover_styles_float\":\"\",\"submit_element_hover_styles_show_advanced_css\":0,\"submit_element_hover_styles_advanced\":\"\",\"cellcid\":\"c3287\",\"admin_label\":\"\",\"id\":4,\"beforeField\":\"\",\"afterField\":\"\",\"value\":\"\",\"label_pos\":\"above\",\"parentType\":\"textbox\",\"element_templates\":[\"submit\",\"button\",\"input\"],\"old_classname\":\"\",\"wrap_template\":\"wrap-no-label\"},{\"objectType\":\"Field\",\"objectDomain\":\"fields\",\"editActive\":false,\"order\":\"5.0\",\"type\":\"recaptcha_v3\",\"label\":\"Hidden\",\"key\":\"recaptcha_v3\",\"default\":\"\",\"admin_label\":\"\",\"drawerDisabled\":false,\"id\":\"46862\",\"beforeField\":\"\",\"afterField\":\"\",\"value\":\"\",\"label_pos\":\"above\",\"parentType\":\"hidden\",\"element_templates\":[\"recaptcha-v3\",\"hidden\",\"input\"],\"old_classname\":\"\",\"wrap_template\":\"wrap-no-label\",\"site_key\":\"6LcqUfIqAAAAAGsZpRiaxTHv4zNpIeTivYdNQsZI\"}];nfForms.push(form);<\/script>\n        ","protected":false},"excerpt":{"rendered":"<p>Security is a crucial aspect of web applications. In&nbsp;NestJS, handling authentication and authorization efficiently is essential to ensure only authenticated users can access protected resources. One of the most common and secure methods to achieve this is using&nbsp;JSON Web Tokens (JWT). In this guide, we&#8217;ll explore how to implement authentication and authorization in a&nbsp;NestJS&nbsp;application using&nbsp;JWT. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1457,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[84,401,402,335,400],"class_list":["post-1456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-dev-centre-house-ireland","tag-json-web-tokens","tag-jwt","tag-nestjs","tag-node-js"],"_links":{"self":[{"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/posts\/1456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/comments?post=1456"}],"version-history":[{"count":2,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/posts\/1456\/revisions"}],"predecessor-version":[{"id":2622,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/posts\/1456\/revisions\/2622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/media\/1457"}],"wp:attachment":[{"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/media?parent=1456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/categories?post=1456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devcentrehouse.eu\/blogs\/wp-json\/wp\/v2\/tags?post=1456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}