Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gmmq 779 fix: 401에러 처리 수정 #264

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: '멘토로 승인되었어요. 다시 로그인 해주세요.',
};