Why CheckAuth triggers twice?
Hello everyone,
I am trying to implement JWT authentication with expiration checking and refreshing using authProvider.
I decided to put logic of JWT checking and refreshing inside CheckAuth method, but faced up the fact this method triggers twice during ordinary getlist request. So in case if refreshing is needed it leads to multiple refresh requests.
Here is my code
5 Replies
foreign-sapphire•2y ago
Hey @Mustdie Could it be related to react strict mode?
https://refine.dev/docs/faq/#why-are-api-calls-triggering-twice
automatic-azureOP•2y ago
No, i checked it already, my index.tsx looks as follows and project started in a build mode
Hey @Mustdie ,
In my opinion, checkAuth is not a suitable place for refresh token (although our documentation says otherwise) because it can be called multiple times. My advice to you is to use this package for axios: https://www.npmjs.com/package/axios-auth-refresh
Hopefully this will be helpful for you. 🎯
npm
axios-auth-refresh
Axios plugin which makes it very easy to automatically refresh the authorization tokens of your clients. Latest version: 3.3.6, last published: 2 months ago. Start using axios-auth-refresh in your project by running
npm i axios-auth-refresh
. There are 104 other projects in the npm registry using axios-auth-refresh.automatic-azureOP•2y ago
ty, i'll try it
Looking closer, seems like axios interceptor is not a best place for token refresh too. In case if we making requests to different resources at the same time (useList and getUserIdentity for example) it leads to multiple refresh token requests.
modern-teal•2y ago
Hello @Mustdie, you can pass axios instance to dataprovider with interceptor, then dataprovider will use the same instance for queries, then it shouldn't be a problem, once token is refreshed, other requests will use new token instantly