You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import axios from "axios";
|
|
|
|
import { getToken, setToken } from "./token";
|
|
|
|
|
|
|
|
export const api = axios.create({
|
|
|
|
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL,
|
|
|
|
});
|
|
|
|
|
|
|
|
api.interceptors.request.use((config) => {
|
|
|
|
const t = getToken();
|
|
|
|
if (t) {
|
|
|
|
config.headers = config.headers ?? {};
|
|
|
|
config.headers.Authorization = `Bearer ${t}`;
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.headers && "Authorization" in config.headers) {
|
|
|
|
delete config.headers.Authorization;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
});
|
|
|
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
(res) => res,
|
|
|
|
(error) => {
|
|
|
|
const url: string | undefined = error?.config?.url;
|
|
|
|
const isAuthRoute =
|
|
|
|
url?.includes("/auth/login") || url?.includes("/auth/signup");
|
|
|
|
if (
|
|
|
|
error?.response?.status === 401 &&
|
|
|
|
typeof window !== "undefined" &&
|
|
|
|
!isAuthRoute
|
|
|
|
) {
|
|
|
|
setToken(null); // 토큰 파기
|
|
|
|
window.location.href = "/login"; // 전역 리다이렉트
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|