From 4583df681bcd2bbc583b929ad1ea81eca7e4f9c7 Mon Sep 17 00:00:00 2001 From: backward99 <86753969+backward99@users.noreply.github.com> Date: Sat, 2 Dec 2023 11:58:43 +0900 Subject: [PATCH] =?UTF-8?q?Gmmq=20779=20fix:=20401=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=88=98=EC=A0=95=20(#264)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 401에러 처리 수정 * fix: alert 창 두번 뜨는 문제 수정 --- src/App.tsx | 13 +------------ src/api/axios.ts | 21 ++++++++++++++++++++- src/constants/errorMessage.ts | 1 + 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 63d3b75f..dc978948 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,10 +3,8 @@ import { createStandaloneToast } from '@chakra-ui/toast'; import { QueryCache, QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { isAxiosError } from 'axios'; import { RouterProvider } from 'react-router-dom'; -import { appPaths } from './config/paths'; import CONSTANTS from './constants/index'; import { ResumeMeErrorResponse } from './types/errorResponse'; -import { deleteCookie } from './utils/cookie'; import router from '~/routes/router'; import theme from '~/theme'; import Fonts from '~/theme/typography/fonts'; @@ -19,16 +17,7 @@ const axiosErrorHandler = (error: Error) => { const { status } = error.response; switch (status) { - case 401: - deleteCookie(CONSTANTS.ACCESS_TOKEN_HEADER); - deleteCookie(CONSTANTS.REFRESH_TOKEN_HEADER); - - alert(CONSTANTS.ERROR_MESSAGES[code]); - - window.location.replace(appPaths.signIn()); - break; - - default: + case 400: if (!(code in CONSTANTS.ERROR_MESSAGES)) { return; } diff --git a/src/api/axios.ts b/src/api/axios.ts index ea9f7105..421669ea 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,6 +1,8 @@ import axios from 'axios'; import { environments } from '~/config/environments'; +import { appPaths } from '~/config/paths'; import CONSTANTS from '~/constants'; +import { ErrorMessage } from '~/types/errorResponse'; import { deleteCookie, getCookie, setCookie } from '~/utils/cookie'; export const resumeMeAxios = axios.create({ @@ -26,7 +28,7 @@ resumeMeAxios.interceptors.response.use( }, async function (error) { const statusCode = error.response.status; - const { code } = error.response.data; + const { code }: { code: ErrorMessage } = error.response.data; if (statusCode === 400 && code === 'INVALID_ACCESS_TOKEN') { deleteCookie(CONSTANTS.ACCESS_TOKEN_HEADER); @@ -47,6 +49,23 @@ resumeMeAxios.interceptors.response.use( setCookie(CONSTANTS.ACCESS_TOKEN_HEADER, newAccessToken); } + if (statusCode === 401) { + if (code === 'MENTOR_ALREADY_APPROVED') { + if (getCookie(CONSTANTS.ACCESS_TOKEN_HEADER)) { + deleteCookie(CONSTANTS.ACCESS_TOKEN_HEADER); + deleteCookie(CONSTANTS.REFRESH_TOKEN_HEADER); + + alert(CONSTANTS.ERROR_MESSAGES[code]); + + window.location.href = appPaths.signIn(); + } + } else { + alert(CONSTANTS.ERROR_MESSAGES.LOGIN_REQUIRED); + + window.location.href = appPaths.signIn(); + } + } + return Promise.reject(error); }, ); diff --git a/src/constants/errorMessage.ts b/src/constants/errorMessage.ts index 8422ae3e..31324533 100644 --- a/src/constants/errorMessage.ts +++ b/src/constants/errorMessage.ts @@ -16,4 +16,5 @@ export const ERROR_MESSAGES = { GPA_ERROR: '최대 학점은 내 학점보다 커야 해요', MENTOR_NOT_FOUND: '멘토를 찾을 수 없어요 :(', NOT_UPDATE_EVENT: '모집 받는 중에는 수정할 수 없어요.', + MENTOR_ALREADY_APPROVED: '멘토로 승인되었어요. 다시 로그인 해주세요.', };