Skip to content

Commit

Permalink
FEAT: access token reissue 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
wet6123 committed Feb 25, 2025
1 parent 4709ed6 commit 6d23400
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions frontend/src/Cabinet/api/axios/axios.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,51 @@ axios.defaults.withCredentials = true;

const instance = axios.create({
baseURL: import.meta.env.VITE_BE_HOST,
});

const reissueInstance = axios.create({
baseURL: import.meta.env.VITE_BE_HOST,
withCredentials: true,
});

const reissueToken = async () => {
try {
const token = (() => {
const pathname = window.location.pathname;
if (pathname.startsWith("/admin")) {
return getCookie("admin_access_token");
}
return getCookie("access_token");
})();

const response = await reissueInstance.post(
"/v5/jwt/reissue",
{},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);

if (response.status === 200) {
return true;
}
return false;
} catch (error) {
console.error("Token reissue failed:", error);
return false;
}
};

instance.interceptors.request.use(async (config) => {
const token = getCookie("admin_access_token") ?? getCookie("access_token");
const token = (() => {
const pathname = window.location.pathname;
if (pathname.startsWith("/admin")) {
return getCookie("admin_access_token");
}
return getCookie("access_token");
})();
config.headers.set("Authorization", `Bearer ${token}`);
return config;
});
Expand All @@ -20,24 +60,40 @@ instance.interceptors.response.use(
(response) => {
return response;
},
(error) => {
// access_token unauthorized
async (error) => {
if (error.response?.status === HttpStatusCode.Unauthorized) {
if (import.meta.env.VITE_IS_LOCAL === "true") {
removeCookie("admin_access_token", {
path: "/",
domain: "localhost",
});
removeCookie("access_token");
const isReissued = await reissueToken();

if (isReissued) {
const originalRequest = error.config;

const newToken = window.location.pathname.startsWith("/admin")
? getCookie("admin_access_token")
: getCookie("access_token");

originalRequest.headers.Authorization = `Bearer ${newToken}`;
return instance(originalRequest);
} else {
removeCookie("admin_access_token", {
path: "/",
domain: "cabi.42seoul.io",
});
removeCookie("access_token", { path: "/", domain: "cabi.42seoul.io" });
const domain =
import.meta.env.VITE_IS_LOCAL === "true"
? "localhost"
: "cabi.42seoul.io";

if (window.location.pathname.startsWith("/admin")) {
removeCookie("admin_access_token", {
path: "/",
domain: domain,
});
} else {
removeCookie("access_token", {
path: "/",
domain: domain,
});
}

window.location.href = "login";
alert(error.response.data.message);
}
window.location.href = "login";
alert(error.response.data.message);
} else if (error.response?.status === HttpStatusCode.InternalServerError) {
logAxiosError(error, ErrorType.INTERNAL_SERVER_ERROR, "서버 에러");
}
Expand Down

0 comments on commit 6d23400

Please sign in to comment.