#how to work with JWT tokens?

7 messages · Page 1 of 1 (latest)

rare shard
#

Hi. I want to deal with authorization by JWT tokens, simplejwt. How to work with these two tokens correctly? It is not entirely clear with obtaining a new access token, do I need to request a new token when the server returns an error due to the fact that the access token has expired, or automatically update this token every N minutes with the help of a task without waiting for the expiration date?
When the refresh token expires, do you need to log in again using a password and login? This is probably a bit inconvenient, for example, a user uses the site and then suddenly he needs to reauthorize, because the validity period of the refresh token has expired.
I am also interested in the option of updating both tokens, not just the access token (ROTATE_REFRESH_TOKEN). When updating both tokens, their validity period is reset to zero and it turns out that due to the fact that the access token has a short life, you have to update and refresh the token, whose life can be several days.

weary prism
# rare shard Hi. I want to deal with authorization by JWT tokens, simplejwt. How to work with...

This is normal. Other applications would use the expiry datetime as a timer countdown. And if its close to the last minute, with no new request, the app should prompt the user that since there's no new activity, the user will logout. If the user decides to continue the session, they should do something like search or get new data or refresh. This action basically refreshes the token and gets a new token that expires later.

#

Its a form of security that prevents users who forgets to logout when not in use (and someone else steps in and does something malicious).

rustic agate
#

It is not entirely clear with obtaining a new access token, do I need to request a new token when the server returns an error due to the fact that the access token has expired
Yes, for example it can be done with Interceptors in Axios library (JS) or hooks in ky library (JS).

When the refresh token expires, do you need to log in again using a password and login?
Yes.

rare shard
#

But I don't think it's safe to store user data. With such data, the token will no longer be needed by an attacker

rustic agate
# rare shard Perhaps this can be prevented by storing the user's authorization data locally a...

Don't do this!
Look at @weary prism response for some ideas how to handle ROTATE_REFRESH_TOKEN option.

if its close to the last minute, with no new request, the app should prompt the user that since there's no new activity, the user will logout.
This part is confusing though, I'm not sure what they mean by "close to the last minute" since refresh token can live for a very long time, so you can prompt user if they'd like to extend the 'session (refresh token)' even 3 days before it expires.
Although I wouldn't play with rotating refresh token, maybe on mobile phone app.