I'm developing website like classified ads with Django REST framework, react and redux. I have a question about authentication with JWT. I want to use djangorestframework_simplejwt for authenticate and I've checked a few tutorial. I saw that many tutorial are checking access token on client side like below
export function isAccessTokenExpired(state) {
if (state.access && state.access.exp) {
return 1000 * state.access.exp - (new Date()).getTime() < 5000
}
return true
}
and refresh token as well. But I don't know why. Because just request new access token with refresh token every time we got HTTP 401 Unauthorized error with expired access token.
The workflow that I thought is
- Send server a request with access token to get page which only authenticated user can see.
- If access token is expired, frontend will get HTTP 401 Unauthorized error.
- Send server a request with refresh token to get new access token, then frontend will store it to localStorage.
- Send a request again.
Is this bad way?
My apologies with my poor English...