{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/guide-to-jwt/","result":{"data":{"markdownRemark":{"id":"0d5ca0dd-a4a1-505b-81bd-072fe40a7d7f","excerpt":"Introduction Have you ever logged into a website by clicking “Login with Google” or “Sign in with Facebook,” without entering your password? Or used a web app…","html":"<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>Have you ever logged into a website by clicking “Login with Google” or “Sign in with Facebook,” without entering your password? Or used a web app that keeps you logged in even after closing your browser? </p>\n<p>These seamless experiences often rely on JSON Web Tokens (JWTs) — a way to authorize users after they have been authenticated.</p>\n<p>In today’s digital landscape, securing user identity and managing access is critical. JWT is a compact and secure method for transmitting claims between parties, typically used after authentication to handle authorization, session management, and secure API access.</p>\n<p>But what exactly is a JWT, how does it work, and why is it important? This blog offers a comprehensive explanation.</p>\n<h2 id=\"what-are-tokens-and-why-are-they-needed\" style=\"position:relative;\"><a href=\"#what-are-tokens-and-why-are-they-needed\" aria-label=\"what are tokens and why are they needed permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What are Tokens and Why Are They Needed?</h2>\n<p>Tokens are digital artifacts used in authentication systems to represent user identity. Instead of maintaining session state on the server, modern applications issue tokens to clients. These tokens are sent along with each request to authorize access to protected resources.</p>\n<p>When a user makes an authenticated request, the token is included in the request header. The server verifies the token’s validity—typically by checking its signature and expiration time. If the token is valid, access is granted. This approach supports stateless and scalable systems, compared to traditional session-based models.</p>\n<p><img src=\"/dd1314dc0f7dba888ea4df48ac00ccbe/token-authentication-method.webp\" alt=\"Illustration depicting authentication in mobile device and PC through token authentication method.\"></p>\n<h2 id=\"what-is-jwt-json-web-token\" style=\"position:relative;\"><a href=\"#what-is-jwt-json-web-token\" aria-label=\"what is jwt json web token permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is JWT (JSON Web Token)?</h2>\n<p><a href=\"https://www.loginradius.com/blog/engineering/jwt/\">A JWT (JSON Web Token)</a> is a compact, self-contained token used to securely transmit claims between parties. JWTs are digitally signed using a secret (with HMAC) or a public/private key pair (with RSA or ECDSA).</p>\n<p>One of the main advantages of JWT authentication is that it doesn't require storing session data on the server—making it ideal for distributed applications.</p>\n<h2 id=\"types-of-jwt\" style=\"position:relative;\"><a href=\"#types-of-jwt\" aria-label=\"types of jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Types of JWT</h2>\n<p>There are two main types of JWT based on how the payload is protected- JWS and JWE. Let’s learn more about them. </p>\n<h3 id=\"jws-json-web-signature\" style=\"position:relative;\"><a href=\"#jws-json-web-signature\" aria-label=\"jws json web signature permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JWS (JSON Web Signature)</h3>\n<p>JWS is a type of JWT where the payload (data) is digitally signed, ensuring integrity and authenticity of the token.</p>\n<ul>\n<li>The payload is Base64URL encoded and signed using a secret or private key. </li>\n<li>It is not encrypted, meaning the contents can be read by anyone who has the token. </li>\n<li>Commonly used in authentication and authorization scenarios like OAuth 2.0 access tokens. </li>\n</ul>\n<p><strong>Use Case:</strong> Verifying that the data has not been tampered with.</p>\n<h3 id=\"jwe-json-web-encryption\" style=\"position:relative;\"><a href=\"#jwe-json-web-encryption\" aria-label=\"jwe json web encryption permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JWE (JSON Web Encryption)?</h3>\n<p>JWE is a type of JWT where the payload is encrypted, ensuring confidentiality in addition to integrity.</p>\n<ul>\n<li>The entire payload is encrypted using a public key or shared secret. </li>\n<li>Only the intended recipient can decrypt and read the token contents. </li>\n<li>Less common than JWS, but ideal for sensitive data transmission. </li>\n</ul>\n<p><strong>Use Case:</strong> Protecting personal or confidential information during transit.</p>\n<h2 id=\"jwt-vs-jws-vs-jwe--comparison-table\" style=\"position:relative;\"><a href=\"#jwt-vs-jws-vs-jwe--comparison-table\" aria-label=\"jwt vs jws vs jwe  comparison table permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JWT vs JWS vs JWE – Comparison Table</h2>\n<table>\n  <tr>\n   <td><strong>Feature</strong>\n   </td>\n   <td><strong>JWT (General)</strong>\n   </td>\n   <td><strong>JWS (Signed JWT)</strong>\n   </td>\n   <td><strong>JWE (Encrypted JWT)</strong>\n   </td>\n  </tr>\n  <tr>\n   <td><strong>Security Focus</strong>\n   </td>\n   <td>Token Format\n   </td>\n   <td>Integrity, authenticity\n   </td>\n   <td>Confidentiality + integrity\n   </td>\n  </tr>\n  <tr>\n   <td><strong>Payload</strong>\n   </td>\n   <td>Not specified\n   </td>\n   <td>Base64URL encoded (readable)\n   </td>\n   <td>Encrypted (not readable)\n   </td>\n  </tr>\n  <tr>\n   <td><strong>Signature</strong>\n   </td>\n   <td>Optional\n   </td>\n   <td>Required\n   </td>\n   <td>Encrypted along with payload\n   </td>\n  </tr>\n  <tr>\n   <td><strong>Encryption</strong>\n   </td>\n   <td>Optional\n   </td>\n   <td>Not encrypted\n   </td>\n   <td>Fully encrypted\n   </td>\n  </tr>\n  <tr>\n   <td><strong>Use Case</strong>\n   </td>\n   <td>ID and Access Tokens\n   </td>\n   <td>OAuth 2.0, OpenID Connect\n   </td>\n   <td>Highly sensitive information\n   </td>\n  </tr>\n</table>\n<p>Note: JWT is the umbrella format. JWS and JWE are implementation types. The most commonly used JWTs in web apps are of the JWS type.</p>\n<h2 id=\"structure-of-jwt\" style=\"position:relative;\"><a href=\"#structure-of-jwt\" aria-label=\"structure of jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Structure of JWT</h2>\n<p>A JWT is composed of three parts:</p>\n<ol>\n<li><strong>Header</strong></li>\n<li><strong>Payload</strong></li>\n<li><strong>Signature</strong></li>\n</ol>\n<p>Each part is Base64URL encoded and separated by a period (.).</p>\n<p><strong>Example:</strong></p>\n<p>&#x3C;Header>.&#x3C;Payload>.&#x3C;Signature></p>\n<h3 id=\"1-header\" style=\"position:relative;\"><a href=\"#1-header\" aria-label=\"1 header permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Header</h3>\n<p>The header typically includes the token type and the signing algorithm being used.</p>\n<p>{</p>\n<p>  \"alg\": \"HS256\",</p>\n<p>  \"typ\": \"JWT\"</p>\n<p>}</p>\n<h3 id=\"2-payload\" style=\"position:relative;\"><a href=\"#2-payload\" aria-label=\"2 payload permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Payload</h3>\n<p>The payload contains the <strong>claims</strong>—statements about an entity (usually the user) and additional metadata.</p>\n<p>{</p>\n<p>  \"iss\": \"<a href=\"https://lrSiteName.hub.loginradius.com/%22\">https://lrSiteName.hub.loginradius.com/\"</a>,</p>\n<p>  \"sub\": \"{uid}\",</p>\n<p>  \"jti\": \"unique string\",</p>\n<p>  \"iat\": 1573849217,</p>\n<p>  \"nbf\": 1573849217,</p>\n<p>  \"exp\": 1573849817,</p>\n<p>  \"Key1\": \"value1\",</p>\n<p>  \"Key2\": \"value2\"</p>\n<p>}</p>\n<h4 id=\"standard-jwt-claims\" style=\"position:relative;\"><a href=\"#standard-jwt-claims\" aria-label=\"standard jwt claims permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Standard JWT Claims</strong></h4>\n<ul>\n<li><strong>iss (Issuer):</strong> Identifies the token issuer (e.g., your LoginRadius domain). </li>\n<li><strong>sub (Subject):</strong> Identifies the user or entity to whom the token refers. </li>\n<li><strong>jti (JWT ID):</strong> Unique identifier for the token, often used to prevent replay attacks. </li>\n<li><strong>iat (Issued At):</strong> Timestamp of when the token was issued. </li>\n<li><strong>nbf (Not Before):</strong> Specifies the time before which the token must not be accepted. </li>\n<li><strong>exp (Expiration):</strong> Sets token expiration—once expired, access is denied. </li>\n</ul>\n<p>Note: The payload is not encrypted by default, and can be decoded by anyone. Do not include sensitive information unless using an encrypted JWT (JWE).</p>\n<h3 id=\"3-signature\" style=\"position:relative;\"><a href=\"#3-signature\" aria-label=\"3 signature permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Signature</h3>\n<p>The signature ensures the token has not been altered. It is created by signing the encoded header and payload using a secret or private key.</p>\n<p>HMACSHA256(</p>\n<p>  base64UrlEncode(header) + \".\" +</p>\n<p>  base64UrlEncode(payload),</p>\n<p>  secret)</p>\n<p>This helps validate the token’s integrity and authenticity.</p>\n<h2 id=\"how-does-jwt-work\" style=\"position:relative;\"><a href=\"#how-does-jwt-work\" aria-label=\"how does jwt work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How Does JWT Work?</h2>\n<p>JWT-based authentication typically follows this flow:</p>\n<ol>\n<li><strong>User Logs In</strong></li>\n</ol>\n<p>The user provides login credentials (e.g., username and password). </p>\n<ol start=\"2\">\n<li><strong>Server Verifies Credentials</strong> </li>\n</ol>\n<p>The server validates the credentials against its data store. </p>\n<ol start=\"3\">\n<li><strong>JWT is Issued</strong> </li>\n</ol>\n<p>Upon successful login, the server issues a JWT signed with a secret/private key (post authentication). </p>\n<ol start=\"4\">\n<li><strong>Client Stores JWT</strong> </li>\n</ol>\n<p>The client stores the token (e.g., in localStorage, sessionStorage, or a secure cookie). </p>\n<ol start=\"5\">\n<li><strong>Token Sent on Requests</strong> </li>\n</ol>\n<p>The client attaches the token to the Authorization header (Bearer &#x3C;token>) in future authorization/authentication API requests.</p>\n<ol start=\"6\">\n<li><strong>Server Validates JWT</strong> </li>\n</ol>\n<p>The server checks the token's signature, expiry, and validity. </p>\n<ol start=\"7\">\n<li><strong>Access is Granted</strong> </li>\n</ol>\n<p>If valid, the user is granted access to protected resources.</p>\n<p>This stateless model makes JWT ideal for scalable web and mobile apps.</p>\n<h2 id=\"how-to-use-oauth-20-with-jwt\" style=\"position:relative;\"><a href=\"#how-to-use-oauth-20-with-jwt\" aria-label=\"how to use oauth 20 with jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Use OAuth 2.0 with JWT</h2>\n<p><a href=\"https://www.loginradius.com/blog/engineering/what-is-oauth2-0/\">OAuth 2.0</a> is an industry-standard protocol for authorization. It enables users to grant third-party apps access to their resources without sharing their credentials.</p>\n<p>When integrated with JWT, OAuth 2.0 uses JWTs as access tokens to represent the user's authorization.</p>\n<p>JWTs are commonly used as OAuth 2.0 access tokens—but not required by the specification. Some providers use opaque tokens instead.</p>\n<p><a href=\"https://www.loginradius.com/resource/whitepaper/secure-api-using-oauth2\"><img src=\"/dce2d7af3a212b2cf75c6b810d4444e2/api-economy.webp\" alt=\"Illustration showing loginradius’s free downloadable resource named- API Economy is transforming digitization- how to secure it using oauth 2.0.\"></a></p>\n<h3 id=\"why-jwts-in-oauth-20\" style=\"position:relative;\"><a href=\"#why-jwts-in-oauth-20\" aria-label=\"why jwts in oauth 20 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why JWTs in OAuth 2.0?</h3>\n<ul>\n<li>JWTs are <strong>self-contained</strong>, carrying all claims. </li>\n<li>They are <strong>digitally signed</strong>, allowing recipients to verify them without contacting the issuer. </li>\n<li>They improve performance by <strong>eliminating database lookups</strong> during request processing. </li>\n</ul>\n<h2 id=\"implementation-of-jwt-using-loginradius-apis\" style=\"position:relative;\"><a href=\"#implementation-of-jwt-using-loginradius-apis\" aria-label=\"implementation of jwt using loginradius apis permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implementation of JWT using LoginRadius APIs</h2>\n<p>To implement JWT with LoginRadius:</p>\n<h3 id=\"step-1-configure-a-jwt-app\" style=\"position:relative;\"><a href=\"#step-1-configure-a-jwt-app\" aria-label=\"step 1 configure a jwt app permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Step 1: Configure a JWT App</strong></h3>\n<p>Set up a JWT app in your <a href=\"https://accounts.loginradius.com/auth.aspx?return_url=https://console.loginradius.com/login&#x26;action=register\">LoginRadius Admin Console</a>. Follow the JWT Admin Console Configuration guide.</p>\n<p><img src=\"/a3ccb47d5a3d66fc01c0eeac6c26328b/jwt-configuration.webp\" alt=\"Illustration showing loginradius admin console with jwt configuration where users can manage access token and refresh token settings.\"></p>\n<h3 id=\"step-2-use-apis-to-retrieve-jwt\" style=\"position:relative;\"><a href=\"#step-2-use-apis-to-retrieve-jwt\" aria-label=\"step 2 use apis to retrieve jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Step 2: Use APIs to Retrieve JWT</strong></h3>\n<p>If you are directly implementing your Login forms or already have an access token or want to generate a JWT based on email/username/Phone number or a password, you can leverage the following APIs:</p>\n<ul>\n<li><a href=\"https://www.loginradius.com/docs/api/v2/single-sign-on/federated-sso/jwt-login/jwt-token-by-access-token/\">JWT Token</a>: This GET API is used to exchange access tokens with your JWT.</li>\n<li><a href=\"https://www.loginradius.com/docs/api/v2/single-sign-on/federated-sso/jwt-login/jwt-token-by-email-and-password/\">JWT Token by Email</a>: This API is used to get a JWT by Email and Password.</li>\n<li><a href=\"https://www.loginradius.com/docs/api/v2/single-sign-on/federated-sso/jwt-login/jwt-token-by-username-and-password/\">JWT Token by Username</a>: This API is used to get JWT by Username and password.</li>\n<li><a href=\"https://www.loginradius.com/docs/api/v2/single-sign-on/federated-sso/jwt-login/jwt-token-by-phone-and-password/\">JWT Token by Phone</a>: This API is used to get JWT by Phone and password.</li>\n</ul>\n<p><strong>API Response Example:</strong></p>\n<p>{</p>\n<p>  \"signature\": \"&#x3C;JWTresponse>\"</p>\n<p>}</p>\n<p>These tokens can then be used in your client app for authenticated requests.</p>\n<h2 id=\"best-practices-for-secure-jwt-authentication\" style=\"position:relative;\"><a href=\"#best-practices-for-secure-jwt-authentication\" aria-label=\"best practices for secure jwt authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Best Practices for Secure JWT Authentication</h2>\n<p>To implement JWT securely, follow these key practices:</p>\n<ol>\n<li><strong>Keep Signing Keys Secure</strong> </li>\n</ol>\n<p>Private keys or secrets used to sign JWTs must be stored securely. </p>\n<ol start=\"2\">\n<li><strong>Avoid Sensitive Data in Payload</strong> </li>\n</ol>\n<p>Payload is only base64 encoded—not encrypted. Do not include passwords, PII, or credentials unless using encrypted JWT (JWE).</p>\n<ol start=\"3\">\n<li><strong>Limit Token Claims</strong> </li>\n</ol>\n<p>Include only essential claims in the token to reduce size and exposure.</p>\n<ol start=\"4\">\n<li><strong>Use HTTPS</strong> </li>\n</ol>\n<p>Always transmit JWTs over HTTPS to prevent man-in-the-middle attacks.</p>\n<ol start=\"5\">\n<li><strong>Set Short Expiry Times</strong> </li>\n</ol>\n<p>Use short exp durations and implement refresh tokens to reduce impact if a token is compromised.</p>\n<ol start=\"6\">\n<li><strong>Implement Token Revocation</strong> </li>\n</ol>\n<p>Use jti with a blacklist or maintain a revocation strategy for enhanced control.</p>\n<h2 id=\"why-are-jwts-important-for-authentication-and-security\" style=\"position:relative;\"><a href=\"#why-are-jwts-important-for-authentication-and-security\" aria-label=\"why are jwts important for authentication and security permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why Are JWTs Important for Authentication and Security?</h2>\n<p>JWTs offer numerous benefits in authentication systems:</p>\n<ul>\n<li><strong>Stateless Authentication</strong> – No need to maintain session state on the server. </li>\n<li><strong>Scalability</strong> – Suitable for microservices and distributed systems. </li>\n<li><strong>Tamper Resistance</strong> – Digitally signed tokens ensure data integrity. </li>\n<li><strong>Performance</strong> – Reduces server load and database dependencies. </li>\n<li><strong>Cross-Platform Support</strong> – Easily used across web, mobile, and API ecosystems.</li>\n<li><strong>Enhanced Security</strong> – Signed tokens ensure authenticity and tamper-proof data. </li>\n<li><strong>Developer Convenience</strong> – Simplifies session management. </li>\n</ul>\n<p>JWTs are widely adopted in OAuth 2.0, OpenID Connect, and API security implementations.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>JWT authentication is a robust, efficient, and secure method for protecting web and mobile applications. By understanding its structure, use cases, and best practices, you can confidently implement JWTs in modern authentication systems.</p>\n<p>Looking to implement JWT in your application? Check out the <a href=\"https://www.loginradius.com/docs/single-sign-on/federated-sso/jwt-login/overview/?q=jwt+\">developer documentation</a> to get started with seamless JWT integration using LoginRadius.</p>\n<h2 id=\"faqs\" style=\"position:relative;\"><a href=\"#faqs\" aria-label=\"faqs permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>FAQ’s</strong></h2>\n<h3 id=\"1-what-is-the-expiration-time-of-jwt-and-what-is-the-measurement-of-time-in-jwt\" style=\"position:relative;\"><a href=\"#1-what-is-the-expiration-time-of-jwt-and-what-is-the-measurement-of-time-in-jwt\" aria-label=\"1 what is the expiration time of jwt and what is the measurement of time in jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>1.</strong> What is the expiration time of JWT, and what is the measurement of time in JWT?</h3>\n<p><strong>A:</strong> By default, the expiry time of a JWT is 600 seconds (10 Minutes). It is shown in the form of seconds in the JWT configuration. The expiry time can be set from 1 second to 2592000 seconds (30 days) as per your use case.</p>\n<h3 id=\"2-what-is-the-difference-between-oauth-and-jwt\" style=\"position:relative;\"><a href=\"#2-what-is-the-difference-between-oauth-and-jwt\" aria-label=\"2 what is the difference between oauth and jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>2.</strong> What is the difference between OAuth and JWT?</h3>\n<p><strong>A:</strong> OAuth is an authorization framework, that allows third-party apps to access user data without exposing their credentials.JWT is used at the end of authentication to securely transmit user info (identity and authorization). Use OAuth for delegated access; use JWT for stateless authentication and API authorization (verifying within your own system).</p>\n<h3 id=\"3-how-many-types-of-jwt-are-there\" style=\"position:relative;\"><a href=\"#3-how-many-types-of-jwt-are-there\" aria-label=\"3 how many types of jwt are there permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>3.</strong> How many types of JWT are there?</h3>\n<p><strong>A:</strong> JWTs mainly come in two types, with one being JSON Web Signature (JWS) and JSON Web Encryption (JWE). In JWS, the token’s content is digitally signed to protect it from tampering during transmission between sender and receiver. While the data is secure from modification, its contents (claims) can still be visible to others. </p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","headings":[{"value":"Introduction","depth":2},{"value":"What are Tokens and Why Are They Needed?","depth":2},{"value":"What is JWT (JSON Web Token)?","depth":2},{"value":"Types of JWT","depth":2},{"value":"JWS (JSON Web Signature)","depth":3},{"value":"JWE (JSON Web Encryption)?","depth":3},{"value":"JWT vs JWS vs JWE – Comparison Table","depth":2},{"value":"Structure of JWT","depth":2},{"value":"1. Header","depth":3},{"value":"2. Payload","depth":3},{"value":"Standard JWT Claims","depth":4},{"value":"3. Signature","depth":3},{"value":"How Does JWT Work?","depth":2},{"value":"How to Use OAuth 2.0 with JWT","depth":2},{"value":"Why JWTs in OAuth 2.0?","depth":3},{"value":"Implementation of JWT using LoginRadius APIs","depth":2},{"value":"Step 1: Configure a JWT App","depth":3},{"value":"Step 2: Use APIs to Retrieve JWT","depth":3},{"value":"Best Practices for Secure JWT Authentication","depth":2},{"value":"Why Are JWTs Important for Authentication and Security?","depth":2},{"value":"Conclusion","depth":2},{"value":"FAQ’s","depth":2},{"value":"1. What is the expiration time of JWT, and what is the measurement of time in JWT?","depth":3},{"value":"2. What is the difference between OAuth and JWT?","depth":3},{"value":"3. How many types of JWT are there?","depth":3}],"fields":{"slug":"/engineering/guide-to-jwt/"},"frontmatter":{"metatitle":"Complete Guide to JWT and How It Works","metadescription":"Learn how JWT (JSON Web Token) works, its structure, and best practices for secure authentication and stateless session management.","description":"Understand JSON Web Tokens (JWT), their compact and secure structure, and their critical role in authentication and authorization. Learn how JWT enables stateless sessions, improves scalability, and secures APIs. This guide explores JWT's working principles and best practices for robust security implementation.","title":"Complete Guide to JSON Web Token (JWT) and How It Works","canonical":null,"date":"April 07, 2025","updated_date":null,"tags":["Oauth","Authorization Code Flow","Authorization","Authentication"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.6528925619834711,"src":"/static/91ea5ae9cba0662d9830e037814a0409/2ad7f/guide-to-jwt.webp","srcSet":"/static/91ea5ae9cba0662d9830e037814a0409/1c9b5/guide-to-jwt.webp 200w,\n/static/91ea5ae9cba0662d9830e037814a0409/f1752/guide-to-jwt.webp 400w,\n/static/91ea5ae9cba0662d9830e037814a0409/2ad7f/guide-to-jwt.webp 800w,\n/static/91ea5ae9cba0662d9830e037814a0409/e7405/guide-to-jwt.webp 1200w,\n/static/91ea5ae9cba0662d9830e037814a0409/d3cba/guide-to-jwt.webp 1600w,\n/static/91ea5ae9cba0662d9830e037814a0409/9f7b4/guide-to-jwt.webp 5115w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Kundan Singh","github":null,"bio":"Director of Product Development @ LoginRadius.","avatar":null}}}},"pageContext":{"id":"0d5ca0dd-a4a1-505b-81bd-072fe40a7d7f","fields__slug":"/engineering/guide-to-jwt/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}