DEV Community

Discussion on: The Ultimate Guide to JWT client side auth (Stop using local storage!!!)

Collapse
 
andreidascalu profile image
Andrei Dascalu • Edited

Dumb question: why do you need to tell the spa when the jwt expires? Presumably the spa has access to the public key which will allow access to the expiration on the token.

Collapse
 
cyberhck profile image
Nishchal Gautam

You don't, you don't even need pub key to know when it expires, client side doesn't need public key at all,

Use a middleware to do refresh token when it's expired, that way if the app is idle, it won't do a refresh

Collapse
 
bahdcoder profile image
Kati Frantz

No dumb questions here Andrei, thanks for asking. This is just for convenience. You can tell the client when it expires, which means the client knows when to refresh, and does not wait for an unexpected expiry before refreshing. Like @nishchal mentioned, you can always wait for the backend to return a 401, and then either automatically logout the user and redirect to the sign in page, or refresh the token and keep the user's session.