diff --git a/frontend/src/features/auth/remotes/auth.ts b/frontend/src/features/auth/remotes/auth.ts index 04e98290..a90bcb96 100644 --- a/frontend/src/features/auth/remotes/auth.ts +++ b/frontend/src/features/auth/remotes/auth.ts @@ -1,4 +1,5 @@ import { client, clientBasic } from '@/shared/remotes/axios'; + interface AccessTokenRes { accessToken: string; } diff --git a/frontend/src/shared/remotes/axios.ts b/frontend/src/shared/remotes/axios.ts index 07f2e134..9f1f2b39 100644 --- a/frontend/src/shared/remotes/axios.ts +++ b/frontend/src/shared/remotes/axios.ts @@ -1,5 +1,5 @@ import axios from 'axios'; -import { postRefreshAccessToken } from './auth'; +import { postRefreshAccessToken } from '@/features/auth/remotes/auth'; import type { AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios'; const { BASE_URL } = process.env; @@ -28,24 +28,24 @@ const setToken = (config: InternalAxiosRequestConfig) => { // 응답 인터셉터 const refreshAccessTokenOnAuthError = async (error: AxiosError) => { - const config = error.config; + const originalRequest = error.config; - if (error.response?.status === 401 && config?.headers.Authorization) { + if (error.response?.status === 401 && originalRequest?.headers.Authorization) { try { - const staleAccessToken = localStorage.getItem('userToken') ?? ''; - const { accessToken } = await postRefreshAccessToken(staleAccessToken); + const { accessToken } = await postRefreshAccessToken(); localStorage.setItem('userToken', accessToken); - config.headers.Authorization = `Bearer ${accessToken}`; + originalRequest.headers.Authorization = `Bearer ${accessToken}`; - return client(config); + return client(originalRequest); } catch { - // window.alert('세션이 만료되었습니다. 다시 로그인 해주세요'); + window.alert('세션이 만료되었습니다. 다시 로그인 해주세요'); } } return Promise.reject(error); }; +clientBasic.interceptors.request.use(setToken); client.interceptors.request.use(setToken); client.interceptors.response.use((response) => response, refreshAccessTokenOnAuthError);