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

401 에러 처리 #276

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/constants/validation/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const ERROR_MESSAGE = {
length: '6~30자 이내로 입력해주세요.',
pattern: '영어, 숫자, 특수문자 중 최소 2가지를 조합해주세요.',
invalidPattern: '사용할 수 없는 문자입니다.',
failed:
'아이디 또는 비밀번호가 잘못 되었습니다. 아이디와 비밀번호를 정확히 입력해주세요.',
},
passwordCheck: {
required: '비밀번호를 확인해주세요.',
Expand Down
19 changes: 17 additions & 2 deletions src/lib/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { getSession } from 'next-auth/react';
import axios, { isAxiosError } from 'axios';
import { getSession, signOut } from 'next-auth/react';
import type { AxiosRequestConfig } from 'axios';

const options: AxiosRequestConfig = {
Expand All @@ -25,4 +25,19 @@ client.interceptors.request.use(
async (error) => await Promise.reject(error),
);

client.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (isAxiosError(error) && error.response?.status === 401) {
alert('세션이 만료되었습니다.\n다시 로그인을 시도해주세요.');
void signOut();
return;
}

return Promise.reject(error);
},
);

export default client;
2 changes: 1 addition & 1 deletion src/pages/account/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Login: NextPage = () => {
});
setError('password', {
type: 'exist',
message: response?.error,
message: ERROR_MESSAGE.password.failed,
});
}
if (response?.ok === true) {
Expand Down