We are building an app and we need to check the JWT token expiration. The question is can we do this from the app side or should the backend handle this?
Note: We are building the application in Flutter.
We are building an app and we need to check the JWT token expiration. The question is can we do this from the app side or should the backend handle this?
Note: We are building the application in Flutter.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Dec 20, 2022, 8:35 AM EST
follow this for flutter
main () { String yourToken = "Your JWT"; bool hasExpired = JwtDecoder.isExpired(yourToken); // You will get a true / false response // true: if the token is already expired // false: if the token is not expired }follow below link
https://pub.dev/packages/jwt_decoder
Deepak TewatiaRank #142 · 16K reputationPosted Dec 20, 2022, 2:09 PM EST
Hi sarfaraj,
It is generally recommended to handle the expiration of JSON Web Tokens (JWTs) on the backend side, rather than the client side. This is because JWTs are signed by the server and contain information that the client should not be able to modify. If you want to handle the expiration of JWTs on the client side, it would be possible for a malicious user to tamper with the JWT and extend its expiration time.
On the backend side, you can set an expiration time when you issue the JWT to the client. The client can store the JWT and send it back to the server with each subsequent request. The server can then verify the JWT and check its expiration Date Time before processing the request. If the JWT has expired, the server can return an appropriate error response to the client.
In your case When making subsequent requests to the backend, retrieve the JWT from storage and include it in the Authorization header of the request.
Thanks
Aravind GovindarajRank #430 · 5.8K reputationPosted Dec 20, 2022, 1:56 PM EST
I prefer to go with Server Side. Make common methods for all the rest API calls and then when the token expires apply refresh token logic...
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 4, 2023, 5:00 PM EST
Checking the expiration of a JWT token should ideally be done on both the client-side and server-side.
On the client-side, you can check the expiration of the token before making any API requests that require authentication. This can help prevent unnecessary API calls and improve the overall performance of your app.
Note that JWT tokens can be manipulated or spoofed on the client-side, so relying solely on client-side checks for token expiration can be a security risk.
It is essential to also have token expiration checks on the server-side. This helps ensure that any API requests that require authentication are only accepted if the JWT token is still valid.