{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/guest-post/jwt-authentication-best-practices-and-when-to-use/","result":{"data":{"markdownRemark":{"id":"68c9955a-2852-5e09-88ce-77f9bc48cbe6","excerpt":"One of the most used authentication standards in web applications is the JSON Web Token standard. It is mostly used for authentication, authorization, and…","html":"<p>One of the most used authentication standards in web applications is the JSON Web Token standard. It is mostly used for authentication, authorization, and information exchange.</p>\n<p>JSON Web tokens are made of three parts separated by dots <code>(.)</code> — and look like this typically: <code>xxxxx.yyyyy.zzzzz</code>. These correspond to the Header, the Payload, and the Signature. You can learn more about <a href=\"https://www.loginradius.com/blog/engineering/jwt/\">JWT tokens here</a>.</p>\n<p>And before using them and continuing to read this article, you might want to check the advantages compared to the session authentication method. You can learn more about <a href=\"https://www.loginradius.com/blog/engineering/guest-post/jwt-vs-sessions/\">JWTs vs. Sessions here</a>.</p>\n<h2 id=\"when-to-use-jwt-authentication\" style=\"position:relative;\"><a href=\"#when-to-use-jwt-authentication\" aria-label=\"when to use 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>When to Use JWT Authentication?</h2>\n<h3 id=\"authentication\" style=\"position:relative;\"><a href=\"#authentication\" aria-label=\"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>Authentication</h3>\n<p>Authentication is done when a client successfully proves its identity via a login endpoint. If it's successful, the server will create JSON Web Token and send it in response to the client.</p>\n<p>The client will use this JWT on every request for a protected resource. </p>\n<h3 id=\"authorization\" style=\"position:relative;\"><a href=\"#authorization\" aria-label=\"authorization 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>Authorization</h3>\n<p>A server built on JWT for authorization will create a JWT when a client logs in. This JWT is signed, so any other party can’t alter it.</p>\n<p>Each time the client has access to protected resources, the server will verify that the JWT’s signature matches its payload and header to determine that the JWT is valid.</p>\n<p>Then if the JWT is successfully verified, it can grant or deny access to the resource. </p>\n<h3 id=\"data-exchanges\" style=\"position:relative;\"><a href=\"#data-exchanges\" aria-label=\"data exchanges 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>Data Exchanges</h3>\n<p>JWT is also a great way to secure information transmission between parties — two servers, for example — and because you can verify the validity of the token (signature, structure, or the standards claimed in the JWT). </p>\n<h2 id=\"when-not-to-use-jwt-authentication\" style=\"position:relative;\"><a href=\"#when-not-to-use-jwt-authentication\" aria-label=\"when not to use 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>When Not to Use JWT Authentication?</h2>\n<h3 id=\"revocable-tokens\" style=\"position:relative;\"><a href=\"#revocable-tokens\" aria-label=\"revocable tokens 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>Revocable Tokens</h3>\n<p>JWT doesn’t require any lookup of the database, so revoking them before the expiration is quite difficult. </p>\n<p>Revocation is very important in many cases.</p>\n<p>For example, when logging out users or banning users, or changing permissions or passwords instantly, if the token hasn't been revoked, it might be possible for the user to continue to make requests even if this user no longer has the required authorization to do so.</p>\n<h3 id=\"sensitive-information\" style=\"position:relative;\"><a href=\"#sensitive-information\" aria-label=\"sensitive information 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>Sensitive Information</h3>\n<p>JWT is usually signed to protect against data manipulation or alteration. With this, the data can be easily read or decoded.</p>\n<p>So, you can’t include sensitive information such as the user’s record or any identifier because the data is not encrypted.</p>\n<h3 id=\"cookie-size-factor\" style=\"position:relative;\"><a href=\"#cookie-size-factor\" aria-label=\"cookie size factor 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>Cookie Size Factor</h3>\n<p>The size of a JWT is greater than the size of a session token. And this can quickly increase linearly as you add more data to the JWT. And because you need to send the JWT at each request, you're increasing the payload size. This can become heavily complex if there is a low-speed internet connection.</p>\n<h2 id=\"jwt-best-practices\" style=\"position:relative;\"><a href=\"#jwt-best-practices\" aria-label=\"jwt best practices 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: Best Practices</h2>\n<h3 id=\"1-jwt-as-access-token\" style=\"position:relative;\"><a href=\"#1-jwt-as-access-token\" aria-label=\"1 jwt as access 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>1) JWT as Access Token</h3>\n<p>JWT can be used as an access token to prevent unwanted access to a protected resource. They're often used as Bearer tokens, which the API will decode and validate before sending a response.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">Authorization: Bearer &lt;token&gt;</span></code></pre>\n<h3 id=\"2-refresh-tokens-logic-with-jwt\" style=\"position:relative;\"><a href=\"#2-refresh-tokens-logic-with-jwt\" aria-label=\"2 refresh tokens logic 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>2) Refresh Tokens Logic with JWT</h3>\n<p>How do you get a new access token if this one is expired? The natural first idea is to log in again. But from a User Experience point, this can be quite painful.</p>\n<p>JWT can be used as <a href=\"https://www.loginradius.com/blog/engineering/guest-post/what-are-refresh-tokens-and-when-to-use-them/\">refresh tokens</a>; these tokens are used to retrieve a new access token.</p>\n<p>For example, when a client requests a protected resource and receives an error, which can mean that the access token has expired, the client can be issued a new access token by sending a request with a refresh token in the headers or the body.\nIf the refresh token is valid, a new access token will be created and sent as a response.</p>\n<p>Note that the refresh token is obtained at authentication and has a bigger lifetime.</p>\n<h3 id=\"3-which-signing-algorithm-to-use\" style=\"position:relative;\"><a href=\"#3-which-signing-algorithm-to-use\" aria-label=\"3 which signing algorithm to use 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) Which Signing Algorithm to Use?</h3>\n<p>Interestingly enough, JWT can be <a href=\"https://www.loginradius.com/blog/engineering/jwt-signing-algorithms/\">signed using many different algorithms</a>. But let’s quickly talk about the <code>alg</code> value in the JWT header. When it’s decoded:</p>\n<p>The <code>alg</code> value in JWT headers simply tells you how the JWT was signed. For example, with an <code>alg</code> value of <code>RS512</code>. </p>\n<p><code>RS512 => RS 512</code> where RS is the signature algorithm and <code>SHA-512</code> is the hashing algorithm.</p>\n<p><code>SHA-512</code> will produce a <code>512-bits</code> hash while <code>SHA-256</code> will produce a <code>256-bit</code> hash. And each of these algorithms gives you 50% of their output size of security level. This means that, for example, <code>SHA-512</code> will provide you with <code>256-bits</code> security.</p>\n<p>In any case, make sure to use a minimum of <code>128-bit</code> security.</p>\n<h3 id=\"4-expiration-issued-time-and-clock-skew\" style=\"position:relative;\"><a href=\"#4-expiration-issued-time-and-clock-skew\" aria-label=\"4 expiration issued time and clock skew 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>4) Expiration, Issued Time, and Clock Skew</h3>\n<p>JWTs are hard to revoke when they are created. Most of the time, you’ll have to wait until expiry. That’s why you should use a short expiration time. </p>\n<p>Additionally, you can implement your own revocation system.</p>\n<p>JWT comes with a time-based claim <code>iat</code> — issued at. It can be used to reject tokens that are too old to be used by the resource server.\nAnd clock skew specifies the allowed time difference (in seconds) between the server and the client clocks when verifying <code>exp</code> and <code>nbf</code> time-based claims. The default recommended default value is 5.</p>\n<h3 id=\"5-jwt-signature\" style=\"position:relative;\"><a href=\"#5-jwt-signature\" aria-label=\"5 jwt 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>5) JWT Signature</h3>\n<p>The last part of a JWT is the signature, which is simply a MAC (or Message Authentication Code). This signature is created by the server using a secret key. This secret key is an important part of the JWT signature.</p>\n<p>There are two things to respect to decrease the probability of a secret key leaking or a successful brute force attack:</p>\n<ul>\n<li>Keep the secret key <strong>secret</strong></li>\n<li>\n<p>The minimum key length must be equal to the size of bits of the hash function used along with the HMAC algorithm.</p>\n<blockquote>\n<p>\"A key of the same size as the hash output (for instance, 256 bits for \"HS256\") or larger MUST be used with this algorithm.\" - <a href=\"https://tools.ietf.org/html/rfc7518#section-3.2\">JSON Web Algorithms (RFC 7518), 3.2 HMAC with SHA-2 Functions</a></p>\n</blockquote>\n</li>\n</ul>\n<h3 id=\"6-where-to-store-the-tokens\" style=\"position:relative;\"><a href=\"#6-where-to-store-the-tokens\" aria-label=\"6 where to store the tokens 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>6) Where to Store the Tokens?</h3>\n<p>The easiest ways to store a token on the client side are <code>localStorage</code> and <code>sessionStorage</code>. However, both are vulnerable to XSS attacks, and <code>sessionStorage</code> is cleaned if the browser is closed.</p>\n<p>A better, secure way is to store JWT in cookies. Cookies are not accessible via JavaScript, they can’t be read and written, and interestingly, they are automatically sent to the server.</p>\n<h3 id=\"7-always-use-https\" style=\"position:relative;\"><a href=\"#7-always-use-https\" aria-label=\"7 always use https 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>7) Always Use HTTPS</h3>\n<p>One of the main benefits of <code>HTTPS</code> is that it comes with security and trust. <code>HTTP</code> path and query parameters are encrypted when using <code>HTTPS</code>.</p>\n<p>Then, there is no risk of someone intercepting the request, particularly the token in transit. These types of attacks are commonly called <strong>MitM</strong> (man in the middle) attacks that can be successful on compromised or insecure networks.</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>This article discussed JWT and some best practices to fully use its potential.</p>\n<p>JWT is simply an authentication standard with its pros and cons. Thus, knowing some best practices can really help you use JWT better.</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  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"When to Use JWT Authentication?","depth":2},{"value":"Authentication","depth":3},{"value":"Authorization","depth":3},{"value":"Data Exchanges","depth":3},{"value":"When Not to Use JWT Authentication?","depth":2},{"value":"Revocable Tokens","depth":3},{"value":"Sensitive Information","depth":3},{"value":"Cookie Size Factor","depth":3},{"value":"JWT: Best Practices","depth":2},{"value":"1) JWT as Access Token","depth":3},{"value":"2) Refresh Tokens Logic with JWT","depth":3},{"value":"3) Which Signing Algorithm to Use?","depth":3},{"value":"4) Expiration, Issued Time, and Clock Skew","depth":3},{"value":"5) JWT Signature","depth":3},{"value":"6) Where to Store the Tokens?","depth":3},{"value":"7) Always Use HTTPS","depth":3},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/guest-post/jwt-authentication-best-practices-and-when-to-use/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"JWT is a common way of implementing authentication in web and mobile apps. Read more to know how you can use JWT and learn the necessary best practices.","title":"JWT Authentication — Best Practices and When to Use","canonical":null,"date":"October 14, 2021","updated_date":null,"tags":["JWT","Authentication"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/dd60f505aa7484a17a6cd0b90a5b0fae/03979/cover-image.png","srcSet":"/static/dd60f505aa7484a17a6cd0b90a5b0fae/f5f11/cover-image.png 200w,\n/static/dd60f505aa7484a17a6cd0b90a5b0fae/6d133/cover-image.png 400w,\n/static/dd60f505aa7484a17a6cd0b90a5b0fae/03979/cover-image.png 800w,\n/static/dd60f505aa7484a17a6cd0b90a5b0fae/aca38/cover-image.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Kolawole Mangabo","github":"koladev32","bio":"He is a full-stack engineer well-versed in React and Django. You can ask him anything about Django Rest and React. He also likes to talk about design, mobile, JavaScript, and productivity.","avatar":null}}}},"pageContext":{"id":"68c9955a-2852-5e09-88ce-77f9bc48cbe6","fields__slug":"/engineering/guest-post/jwt-authentication-best-practices-and-when-to-use/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}