Skip to content

Commit

Permalink
Disable retry logic for 401 errors during API call (#325)
Browse files Browse the repository at this point in the history
* Disable retrying fetching user when token is expired

* Disable retrying fetching when the error status code is 401
  • Loading branch information
devleejb authored Aug 31, 2024
1 parent 75df2ae commit 02e92de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 9 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ function App() {
},
}),
defaultOptions: {
queries: {
retry: (failureCount, error) => {
if (axios.isAxiosError(error) && error.response?.status === 401) {
return false;
}

return failureCount < 3;
},
},
mutations: {
onError: handleError,
},
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/hooks/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ export const useGetUserQuery = () => {
const dispatch = useDispatch();
const authStore = useSelector(selectAuth);

if (authStore.accessToken) {
axios.defaults.headers.common["Authorization"] = `Bearer ${authStore.accessToken}`;
}

const query = useQuery({
queryKey: generateGetUserQueryKey(authStore.accessToken || ""),
enabled: Boolean(authStore.accessToken),
queryFn: async () => {
axios.defaults.headers.common["Authorization"] = `Bearer ${authStore.accessToken}`;
const res = await axios.get<GetUserResponse>("/users");
return res.data;
},
Expand Down

0 comments on commit 02e92de

Please sign in to comment.