Skip to main content
This guide demonstrates how to verify that the JWT expiration mechanism works as expected. A JSON Web Token (JWT) contains an expiration time that limits the validity of a user session. In our implementation, the default access token expires after 30 minutes. If a user attempts to access protected endpoints with the token beyond this duration, an error will be returned.
The default token expiration is 30 minutes, ensuring enhanced security. However, for testing purposes, the expiration time is temporarily shortened.

Creating an Access Token with a 30-Minute Expiration

Below is the Python code that creates an access token with a 30-minute expiration:
During normal operation, your application logs may resemble the following:

Testing with a One-Minute Token Expiration

To expedite the testing process without waiting for 30 minutes, you can temporarily modify the token’s expiration time to one minute. With this change, after one minute, the token will expire and any attempt to access protected endpoints will result in an “Unauthorized” error. Here’s the modified code for testing purposes:
After updating the code, log in with a user so that the token is valid for one minute. To verify its functionality, you can retrieve some posts. For example, the following POST payload might be used when testing the “get posts” endpoint:
Once you make a successful request, wait for a full minute. If you try accessing the posts again with the expired token, you should receive an error. A successful GET request might return a response similar to:
And when the token has expired, the error response will be:

Reverting to a Longer Expiration Time

After testing the token expiration, it is important to restore the intended expiration duration. For practical use, you might change the token’s expiration to 60 minutes to minimize the need for frequent token refreshes. Below is the updated code reflecting this change:
Following the change, your application logs might appear as follows:
This confirms that the JWT expiration functionality is working as expected. Transcribed by Otter.ai

Watch Video