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

[ Feat ] 로그인 관련 에러 처리 (존재하지 않는 회원일 경우, 토큰 만료) #333

Merged
merged 3 commits into from
Nov 4, 2024
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
9 changes: 6 additions & 3 deletions src/pages/login/hooks/useGoogleLoginMutation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMutation } from '@tanstack/react-query';
import { loginAxios } from '../apis/loginAxios';
import { useNavigate } from 'react-router-dom';
import { setRole, setToken } from '@utils/storage';
import { clearStorage, setRole, setToken } from '@utils/storage';

interface useGoogleLoginPropType {
role?: string;
Expand All @@ -22,8 +22,11 @@ const useGoogleLoginMutation = ({ role }: useGoogleLoginPropType) => {
// 회원가입
navigate(role === 'SENIOR' ? '/seniorOnboarding' : '/juniorOnboarding');
} else {
// 로그인인데, role 정보를 서버에서 받지 못한 상황
console.error('🔴 로그인 과정에서 Role 정보를 서버에서 받지 못했어요.');
// 존재하지 않는 계정으로 로그인을 시도했을 경우
console.error('🔴 존재하지 않는 계정');
alert('존재하지 않는 계정이예요. 회원가입을 진행해주세요.');
navigate('/');
clearStorage();
}
},
onError: (error) => {
Expand Down
14 changes: 13 additions & 1 deletion src/utils/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getToken } from '@utils/storage';
import { clearStorage, getToken } from '@utils/storage';
import * as _axios from 'axios';

const baseUrl = import.meta.env.VITE_APP_API_BASE_URL;
Expand Down Expand Up @@ -29,3 +29,15 @@ authAxios.interceptors.request.use(
return Promise.reject(error);
}
);

authAxios.interceptors.response.use(
(res) => res,
async (err) => {
if (err.response.data.code === '40076') {
alert(`세션이 만료되었습니다.\n다시 로그인해주세요.`);
clearStorage();
location.href = '/';
}
return Promise.reject(err);
}
);
5 changes: 5 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const setRole = (role: string) => {
export const getRole = () => {
return localStorage.getItem('seonyakRole');
};

export const clearStorage = () => {
localStorage.removeItem('seonyakToken');
localStorage.removeItem('seonyakRole');
};
Loading