Let's say we have short-lived access token (15 minutes) and long-term refresh token (7 days).
When should we ask backend to refresh access token?
I see two options:
- After user logs in we start a countdown to automatically refresh token one minute before access token expires.
- We don't implement timer and we try to refresh access token ONLY if we get 401 response from backend.
In first option I see one advantage - if access token and refresh token will expired AND user stays on the page, not taking any action, he also doesn't send any http request than the timer still works and user is logged out automatically.
In second option - if access token and refresh token will expired user will be logged out ONLY if he will make some action on page for example: leave a page or make a http request. If he will stay on page he won't be logged out automatically.
What is a better implementation on frontend than?