2026-05-30 21:18:13 +09:00
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
|
|
// 공통 axios 인스턴스
|
|
|
|
|
const http = axios.create({
|
|
|
|
|
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 요청 인터셉터 (예: 토큰 주입)
|
|
|
|
|
http.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
const token = localStorage.getItem('token')
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
(error) => Promise.reject(error),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 응답 인터셉터 (공통 에러 처리)
|
|
|
|
|
http.interceptors.response.use(
|
|
|
|
|
(response) => response.data,
|
|
|
|
|
(error) => {
|
|
|
|
|
const status = error.response?.status
|
|
|
|
|
if (status === 401) {
|
2026-05-31 15:42:52 +09:00
|
|
|
// 세션 만료/무효: 클라이언트 저장값 정리 후 로그인 팝업 트리거
|
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
localStorage.removeItem('auth_user')
|
|
|
|
|
window.dispatchEvent(new CustomEvent('auth:unauthorized'))
|
2026-05-30 21:18:13 +09:00
|
|
|
}
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default http
|