DEV Community

Ahmed Rakan
Ahmed Rakan

Posted on

How to Handle Changes in JWT Token Claims

Here’s an improved version of the blog:


How to Handle Changes in Stateless JWT Token State

Introduction to JWT

JSON Web Tokens (JWT) is an open standard that defines a compact and self-contained method for securely transmitting information as a JSON object between parties. These tokens are digitally signed, ensuring that their contents can be verified and trusted. If tampered with, the signature verification fails, making JWT inherently secure in terms of integrity.

For a deeper understanding, visit JWT.io Introduction.

JWT Usage in Applications

JWTs are widely used in stateless architectures, such as microservices, where a shared state must be transmitted across decoupled services. They are particularly advantageous because they embed state information, called "claims," within the token payload.

Claims are statements about a user or entity, such as:

  • Subscription status (e.g., free or premium).
  • Membership type or role.
  • Tenant-specific data in multi-tenant applications (e.g., organization ID, number of allowed accounts, etc.).

This ability to store contextual information in a stateless manner makes JWT an excellent choice for scenarios where scalability and simplicity are paramount.

The Problem: Handling State Changes

In many real-world scenarios, the state represented within a JWT can become outdated due to user actions that invalidate the token's payload. Common examples include:

  • Upgrading or downgrading a subscription.
  • Changing roles or permissions.
  • Revoking access to certain features or resources.

While short-lived tokens mitigate this issue by requiring periodic re-authentication, long-lived tokens introduce a challenge: how do we handle state changes without forcing users to log out?

Solution 1: Refreshing the Token Without Logging Out

To address this, a practical approach is to refresh the token dynamically when a state change occurs. Instead of invalidating the session and forcing the user to re-login, you can:

  1. Generate a new token: Create a new JWT reflecting the updated state.
  2. Return the token in the response: Send the new token to the client in the HTTP response body or headers.
  3. Update the client-side token: The client application can then update the stored token (e.g., in local storage or memory) and continue the session seamlessly.

Solution 2: Refreshing the Token with /refresh-token

To address this, a practical approach is to refresh the token dynamically when a state change occurs. Instead of invalidating the session and forcing the user to re-login, we can allow the client to ask for new token with updated details:

  1. User is already authenticated: User must be already authenticated to our services when sending the request.
  2. Refresh endpoint: Refresh endpoint return the response with a new token.
  3. User gets new token: The token gets refreshed and sent to the client and set to it.

In both cases we need to revoke the token, we can revoke the token in two ways, the first way by having a deny list using cache storage like Redis. The other way is by going back to short-lived tokens and refreshing them periodically.

The deny list is more scalable because we can store tokens only for the duration of their expiry date.

Top comments (1)

Collapse
 
kamalhinduja profile image
Kamal Hinduja

Thanks for Sharing information