Skip to content

Commit

Permalink
fix: handle unauthorized request
Browse files Browse the repository at this point in the history
  • Loading branch information
izzuzantyaf committed Jan 5, 2025
1 parent 3ea9c89 commit 49510c3
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/common/helpers/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCookie, isBrowser } from "@/common/utils";
import { clearCookie, getCookie, isBrowser } from "@/common/utils";
import {
notifications as _notifications,
NotificationData,
} from "@mantine/notifications";
import axios from "axios";
import axios, { AxiosError, HttpStatusCode } from "axios";
import { TbCheck, TbX } from "react-icons/tb";

export const apiClient = axios.create({
Expand All @@ -13,6 +13,29 @@ export const apiClient = axios.create({
},
});

apiClient.interceptors.response.use(
function (response) {
return response;
},
function (error) {
function handleUnauthorizedError(error: AxiosError) {
if (
error instanceof AxiosError &&
error.status === HttpStatusCode.Unauthorized &&
isBrowser()
) {
clearCookie("access_token");

window.location.reload();
}
}

handleUnauthorizedError(error);

return Promise.reject(error);
}
);

export const notifications = {
..._notifications,
show(props: NotificationData & { type: "success" | "error" }) {
Expand Down

0 comments on commit 49510c3

Please sign in to comment.