In my scenario, to avoid the expiration of token I decide to call the refresh token for every API calls (Adding it in the Interceptor axiosInstance.ts). I don’t know whether the approach is correct or not. If it’s correct where do I add it in my React Typescript application
Need to call the refresh token for all API calls expect some url’s.
3
Answers
Well refresh tokens are used to check whether the token is needed to again issued or not for all the authorized routes where it uses the token to actually validate the user and then authorize the request. So personally i think that its better to only pass it the end points where this kind of authorization is needed and check for it like get values which uses the authorization or post etc. But in some scenarios like whereas you want to logout the user depending on some values its better to just pass it in always and then check whether the token is expired or not. This is just my personal approach.
No this approach is not efficient i have did in this below steps
login request after successful receive authentication token and refresh token store in global place
when i request data from api using authentication token some time api return 401 because token has expired then renew auth token using refresh token
You should only call the refresh endpoint only when the token is expired or you have gotten 401 response, you can check this using axios interceptors.
if it is a jwt token you can check if the token is expired using this function
Your can call that function inside another function that returns an access token
You can call this function inside axios interceptor to check if the response is 401 , i use a react hook to accomplish that, but you can use it any other way
so i create the axios instance in one place and import it every where to use it like