The default token expiration is 30 minutes, ensuring enhanced security. However, for testing purposes, the expiration time is temporarily shortened.
Advanced FastAPI
Testing Expired Token
This guide demonstrates how to verify the JWT expiration mechanism and test access tokens with varying expiration times.
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.
During normal operation, your application logs may resemble the following:
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:
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