Skip to content

Commit

Permalink
fix: 401에러 처리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
backward99 committed Dec 1, 2023
1 parent 970ddd8 commit 6ab28ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 0 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -19,15 +17,6 @@ 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:
if (!(code in CONSTANTS.ERROR_MESSAGES)) {
return;
Expand Down
13 changes: 12 additions & 1 deletion src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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);
Expand All @@ -47,6 +49,15 @@ resumeMeAxios.interceptors.response.use(
setCookie(CONSTANTS.ACCESS_TOKEN_HEADER, newAccessToken);
}

if (statusCode === 401) {
deleteCookie(CONSTANTS.ACCESS_TOKEN_HEADER);
deleteCookie(CONSTANTS.REFRESH_TOKEN_HEADER);

alert(CONSTANTS.ERROR_MESSAGES[code]);

window.location.href = appPaths.signIn();
}

return Promise.reject(error);
},
);
1 change: 1 addition & 0 deletions src/constants/errorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const ERROR_MESSAGES = {
GPA_ERROR: '최대 학점은 내 학점보다 커야 해요',
MENTOR_NOT_FOUND: '멘토를 찾을 수 없어요 :(',
NOT_UPDATE_EVENT: '모집 받는 중에는 수정할 수 없어요.',
MENTOR_ALREADY_APPROVED: '멘토로 승인되었어요. 다시 로그인 해주세요.',
};

0 comments on commit 6ab28ae

Please sign in to comment.