[인증 오류 처리 개선] API 호출 시 인증 오류 발생 시 사용자 로그아웃 처리 및 인증 오류 페이지 추가. auth.global.ts에서 공개 라우트에 인증 오류 페이지 포함, 사용자 세션 정리 로직 개선.
This commit is contained in:
@@ -75,11 +75,26 @@ export const useApi = async <T>(
|
||||
...(headers && { headers }),
|
||||
};
|
||||
|
||||
return ($api as any)(path, apiOpts).catch((error: any) => {
|
||||
return ($api as any)(path, apiOpts).catch(async (error: any) => {
|
||||
const status = error.response?.status;
|
||||
const message = error.response._data.message;
|
||||
|
||||
if (
|
||||
status === 401 &&
|
||||
["JWT_TOKEN_EXPIRED", "INVALID_CLIENT_IP", "JWT_TOKEN_NULL"].includes(
|
||||
message
|
||||
)
|
||||
) {
|
||||
const userStore = useUserStore();
|
||||
userStore.user = null;
|
||||
userStore.isLoggedIn = false;
|
||||
await navigateTo(`/auth-error?type=${message}`);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// 사용자에게 알림 표시
|
||||
if (showAlert) {
|
||||
const status = error.response?.status;
|
||||
let message =
|
||||
let description =
|
||||
status === 404
|
||||
? "요청한 리소스를 찾을 수 없습니다."
|
||||
: status === 500
|
||||
@@ -88,10 +103,10 @@ export const useApi = async <T>(
|
||||
|
||||
// 서버에서 온 에러 메시지가 있으면 우선 사용
|
||||
if (error.response?._data?.description) {
|
||||
message = error.response._data.description;
|
||||
description = error.response._data.description;
|
||||
}
|
||||
|
||||
alert(message);
|
||||
alert(description);
|
||||
}
|
||||
|
||||
// 에러 처리 방식에 따라 반환
|
||||
|
||||
Reference in New Issue
Block a user