diff --git a/src/api/core/index.ts b/src/api/core/index.ts index 28900307..ec080064 100644 --- a/src/api/core/index.ts +++ b/src/api/core/index.ts @@ -2,6 +2,8 @@ import axios from 'axios'; import { CommonErrorResponse, CommonSuccessResponse } from '@/types/service/common'; +import { API } from '..'; + export const baseAPI = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, timeout: 20000, @@ -46,17 +48,25 @@ baseAPI.interceptors.response.use( const status = error.response?.status ?? errorResponse.status; const isServer = typeof window === 'undefined'; + const originalRequest = error.config; - if (status === 401) { - if (isServer) { - const { redirect } = await import('next/navigation'); - redirect('/login'); - } else { - if (window.location.pathname === '/login') { - throw errorResponse; + if (status === 401 && !originalRequest._retry) { + originalRequest._retry = true; + try { + await API.authService.refresh(); + return baseAPI(originalRequest); + } catch (refreshError) { + if (isServer) { + const { redirect } = await import('next/navigation'); + redirect('/login'); + } else { + if (window.location.pathname === '/login') { + throw errorResponse; + } + const currentPath = window.location.pathname + window.location.search; + window.location.href = `/login?error=unauthorized&path=${encodeURIComponent(currentPath)}`; } - const currentPath = window.location.pathname + window.location.search; - window.location.href = `/login?error=unauthorized&path=${encodeURIComponent(currentPath)}`; + throw refreshError; } } if (status === 404) { diff --git a/src/api/service/auth-service/index.ts b/src/api/service/auth-service/index.ts index 821d349e..364062cc 100644 --- a/src/api/service/auth-service/index.ts +++ b/src/api/service/auth-service/index.ts @@ -28,7 +28,11 @@ export const authServiceRemote = () => ({ // 액세스 토큰 재발급 refresh: async () => { - const data = await api.post('/auth/refresh'); + const data = await api.post( + '/auth/refresh', + {}, + { _retry: true, withCredentials: true }, + ); setAccessToken(data.accessToken, data.expiresIn); return data;