Skip to content

Commit

Permalink
Gmmq 779 fix: 401에러 처리 수정 (#264)
Browse files Browse the repository at this point in the history
* fix: 401에러 처리 수정

* fix: alert 창 두번 뜨는 문제 수정
  • Loading branch information
backward99 authored Dec 2, 2023
1 parent 970ddd8 commit 4583df6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
13 changes: 1 addition & 12 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,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;
}
Expand Down
21 changes: 20 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,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);
},
);
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 4583df6

Please sign in to comment.