Skip to content

Commit 9ee5304

Browse files
committed
fix: 에러코드 타입 선언
1 parent 9af50e7 commit 9ee5304

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/shared/types/api/http-client.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import useAuthStore from '../../../app/provider/authStore';
55
// 로그아웃 처리 및 리다이렉트
66
function logoutAndRedirect(error: unknown) {
77
const authStore = useAuthStore.getState();
8+
// Zustand persist가 자동으로 localStorage를 정리하므로 수동 정리 불필요
89
authStore.logout();
9-
localStorage.removeItem('auth-storage');
1010
authStore.openModal();
1111

1212
console.log('logoutAndRedirect', error);
@@ -69,14 +69,10 @@ axiosClient.interceptors.response.use(
6969
originalRequest._retry = true;
7070

7171
try {
72-
await axios.post(
73-
`${import.meta.env.VITE_API_BASE_URL}/api/v1/oauth/reissue`,
74-
{},
75-
{ withCredentials: true }
76-
);
72+
await axios.post(`${import.meta.env.VITE_API_BASE_URL}/api/v1/oauth/reissue`, {}, { withCredentials: true });
7773
// 새 토큰이 쿠키에 재설정되었으므로 원래 요청 재시도
7874
return axiosClient(originalRequest);
79-
} catch (refreshError) {
75+
} catch (refreshError: unknown) {
8076
// 리프레시 실패 시 로그아웃 처리
8177
logoutAndRedirect(refreshError);
8278

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { axiosClient } from './http-client';
22
import useAuthStore from '../../../app/provider/authStore';
33

4+
// 타입 가드 함수
5+
function isTokenError(error: unknown): error is { code: string; status?: number } {
6+
return error !== null && typeof error === 'object' && 'code' in error && typeof (error as any).code === 'string';
7+
}
8+
49
// 토큰 유효성 검증 함수 (httpOnly 쿠키 사용)
510
export const validateToken = async (): Promise<boolean> => {
611
try {
712
// 토큰 유효성 검증을 위한 API 호출 (사용자 정보 조회)
813
await axiosClient.get('/users');
914
return true;
10-
} catch (error: any) {
15+
} catch (error: unknown) {
1116
// 토큰이 만료되었거나 유효하지 않은 경우
12-
if (error.code === 'TOKEN4001' || error.code === 'TOKEN4004' || error.status === 401) {
17+
if (isTokenError(error) && (error.code === 'TOKEN4001' || error.code === 'TOKEN4004' || error.status === 401)) {
1318
return false;
1419
}
1520
// 다른 에러의 경우 토큰이 유효하다고 간주
@@ -20,18 +25,18 @@ export const validateToken = async (): Promise<boolean> => {
2025
// 앱 시작 시 토큰 검증 및 자동 로그아웃
2126
export const initializeAuth = async () => {
2227
const authStore = useAuthStore.getState();
23-
28+
2429
// 로그인 상태가 아닌 경우 검증하지 않음
2530
if (!authStore.isLoggedIn) {
2631
return;
2732
}
2833

2934
const isValid = await validateToken();
30-
35+
3136
if (!isValid) {
3237
// 토큰이 유효하지 않으면 로그아웃 처리
38+
// Zustand persist가 자동으로 localStorage를 정리하므로 수동 정리 불필요
3339
authStore.logout();
34-
localStorage.removeItem('auth-storage');
3540
authStore.openModal();
3641
}
37-
};
42+
};

0 commit comments

Comments
 (0)