Can jwts be invalidated. I have heard it can be used even after logout till the expiration time

Basically NOO

JWTs (JSON Web Tokens) are self-contained and stateless tokens, which means that once they are issued and signed by the server, they cannot be invalidated or revoked before their expiration time, as there is no central authority or server-side state involved in token verification.

The fact that JWTs cannot be invalidated before their expiration time is both an advantage and a disadvantage, depending on how you look at it:

Advantages of JWTs not being invalidated:

  1. Stateless nature: Since JWTs are stateless, the server does not need to maintain a session store or database to keep track of issued tokens. This makes JWTs suitable for scalable and distributed architectures, such as microservices and serverless applications.
  2. Reduced database queries: With traditional session-based authentication, the server needs to query a session store or database on every request to verify the session's validity. In contrast, JWTs contain all the necessary information, eliminating the need for database queries for session verification.

Disadvantages of JWTs not being invalidated:

  1. Longer token lifespan: If a JWT is compromised or stolen, it remains valid until it expires. Depending on the expiration time set for the token, this can be a security concern, especially if the token has a long lifespan.
  2. Revocation challenges: Since JWTs cannot be invalidated directly, handling token revocation (e.g., user logout) becomes more challenging. You may need to resort to token blacklists or maintain a token revocation list on the server side, which can introduce complexities and overhead.

To address the limitations of JWTs not being invalidated, you can consider the following approaches:

  1. Shorter expiration times: Set a relatively short expiration time for your JWTs, so even if they are compromised, their validity period is limited.
  2. Use token blacklists or revocation lists: Maintain a list of revoked tokens on the server side, and check each incoming token against this list. While this introduces some level of statefulness, it can help handle token revocation.
  3. Consider token rotation: Rotate or refresh JWTs periodically to minimize the impact of a compromised token. This can be done by issuing a new token with a renewed expiration time upon specific events or at regular intervals.
  4. Implement a more comprehensive authentication strategy: Depending on your security requirements, you might need to combine JWT-based authentication with other mechanisms like OAuth2, Single Sign-On (SSO), or Multi-Factor Authentication (MFA) to provide additional layers of security and manage authentication more effectively.

Remember that while JWTs are a popular and useful tool for many authentication scenarios, their usage should be carefully planned, and security considerations should be taken into account when designing your authentication system.

safaldas

safaldas