What's the correct approach to use axios response interceptors with react-query having retries enabled #3653
Replies: 2 comments 4 replies
-
having the refresh token based logic in an axios intercepter seems like the right way to go. We've thought about adding async retries, but we've deemed that it is mainly out of scope for react-query and should be solved on the actual network interaction layer. Not sure why it wouldn't work tbh - can you show your behaviour in a codesandbox reproduction please? |
Beta Was this translation helpful? Give feedback.
-
@adityawarrier I have a good experience with RQ + axios + axios-auth-refresh. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, I'm noticing a weird issue wherein I'm trying to implement a simple refresh token flow in a react-native application making use of react-query and an axios based API client.
The query,
useQuery(QueryKeys.FetchUserDetail, UserRepository.fetchUserDetails)
The Repository which makes the API call,
const fetchUserDetails = async (): Promise<UserInfo> => { const response = await BaseClient.get(ENDPOINTS.userDetail); return response.data; };
The above API fails as an expired token is being used as the Bearer, and ,as expected react-query does the job of retrying 3 times as per my configuration,
const queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 3, }, }, });
My initial idea was to write some refreshToken based logic in the BaseClient as a response interceptor, however what I'm noticing is that even though the API is being called a total of 4 times with the back-off delay, my response interceptor only fires off once corresponding to the initial API call. The retry calls never invoke the response interceptors even though they are also failing with a 401.
I'm a bit confused as to how to go about this, as my idea is to wait for all the retries to end and then ensure that my refresh token logic runs via the interceptor.
Beta Was this translation helpful? Give feedback.
All reactions