diff --git a/package.json b/package.json index 700e9f60..49d669a3 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,9 @@ }, "msw": { "workerDirectory": [ - "src/Mocks" + "src/Mocks", + "public" ] - } + }, + "packageManager": "yarn@1.22.19" } diff --git a/src/Mocks/browser.ts b/src/Mocks/browser.ts index 44f78f43..0a564278 100644 --- a/src/Mocks/browser.ts +++ b/src/Mocks/browser.ts @@ -1,4 +1,4 @@ -// import { setupWorker } from 'msw/browser'; -// import { handlers } from './handlers'; +import { setupWorker } from 'msw/browser'; +import { handlers } from './handlers'; -// export const worker = setupWorker(...handlers); +export const worker = setupWorker(...handlers); diff --git a/src/Mocks/handlers/auth.ts b/src/Mocks/handlers/auth.ts index 6b49b85b..17b36039 100644 --- a/src/Mocks/handlers/auth.ts +++ b/src/Mocks/handlers/auth.ts @@ -1,9 +1,18 @@ import { HttpResponse, http } from 'msw'; import { mockUsers } from '../data/mockAuth'; -const baseURL = import.meta.env.VITE_MOCK_API_URL; +const baseURL = import.meta.env.VITE_BASE_API_URL; const getUser = http.get(`${baseURL}/api/users/me`, () => { + const isAuthenticated = document.cookie.includes('Authorization='); + + if (!isAuthenticated) { + return new HttpResponse(JSON.stringify({ data: null, message: '로그인이 필요합니다.' }), { + status: 401, + statusText: 'Unauthorized', + }); + } + return new HttpResponse(JSON.stringify({ data: mockUsers[1], message: 'Success' }), { status: 200, statusText: 'OK', diff --git a/src/index.tsx b/src/index.tsx index f6e0161b..bf5c4813 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,10 +1,5 @@ import ReactDOM from 'react-dom/client'; -import { StrictMode } from 'react'; import App from './App.tsx'; import './App.css'; -ReactDOM.createRoot(document.getElementById('root')!).render( - - - , -); +ReactDOM.createRoot(document.getElementById('root')!).render(); diff --git a/src/utils/axios.ts b/src/utils/axios.ts index 3f6a647b..e344ce59 100644 --- a/src/utils/axios.ts +++ b/src/utils/axios.ts @@ -1,4 +1,6 @@ -import axios, { AxiosRequestConfig } from 'axios'; +import { logOut } from '@/Apis/auth'; +import { HttpStatus } from '@/Constants/StatusCodes'; +import axios, { AxiosError, AxiosRequestConfig } from 'axios'; export const createClient = (config?: AxiosRequestConfig) => { const axiosInstance = axios.create({ @@ -18,7 +20,14 @@ export const createClient = (config?: AxiosRequestConfig) => { (response) => { return response; }, - (error) => { + async (error: AxiosError) => { + if (error.response?.status === HttpStatus.UNAUTHORIZED) { + try { + const data = await logOut(); // 로그아웃 통해 쿠키 제거 + if (data) window.location.href = '/'; + } catch {} + } + // Login Provider에서 에러 핸들링하도록 reject return Promise.reject(error); }, );