I am having a problem with the RTK query interceptor, I implemented Automatic re-authorization by extending fetchBaseQuery per RTK query documentation and now I have a problem that if multiple requests get fired and the token is not valid all of them will get 401 as a response and all of them will try to refresh a token which will result in the first one being successful and other ones will fail and then the else will fire and return the user to log in screen.
Is there a way to prevent this?
let result = await baseQuery(args, api, extraOptions)
if (result.error && result.error.status === 401) {
// try to get a new token
const refreshResult = await baseQuery(
{
url: `http://.../${refreshToken}`,
method: 'POST'
},
api,
extraOptions
);
if (refreshResult.data) {
// store the new token
api.dispatch(tokenReceived(refreshResult.data))
// retry the initial query
result = await baseQuery(args, api, extraOptions)
} else {
api.dispatch(loggedOut())
}
}
return result