[agent-pipeline] pipe-20260317084014-ydap round-3

This commit is contained in:
DDD1542 2026-03-17 18:25:36 +09:00
parent ad48b22770
commit cfd7ee9fce
1 changed files with 17 additions and 45 deletions

View File

@ -2,12 +2,13 @@
import { useState, useEffect, useCallback } from "react";
import { useRouter } from "next/navigation";
import { LoginFormData, LoginResponse } from "@/types/auth";
import { LoginFormData } from "@/types/auth";
import { AUTH_CONFIG, FORM_VALIDATION } from "@/constants/auth";
import { API_BASE_URL } from "@/lib/api/client";
import { apiCall } from "@/lib/api/client";
/**
*
* API lib/api/client의 apiCall(Axios) (fetch )
*/
export const useLogin = () => {
const router = useRouter();
@ -73,67 +74,34 @@ export const useLogin = () => {
}, [formData]);
/**
* API
*/
const apiCall = useCallback(async (endpoint: string, options: RequestInit = {}): Promise<LoginResponse> => {
// 로컬 스토리지에서 토큰 가져오기
const token = localStorage.getItem("authToken");
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
credentials: "include",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
...(token && { Authorization: `Bearer ${token}` }),
...options.headers,
},
...options,
});
const result = await response.json();
return result;
}, []);
/**
*
* (apiCall )
*/
const checkExistingAuth = useCallback(async () => {
try {
// 로컬 스토리지에서 토큰 확인
const token = localStorage.getItem("authToken");
if (!token) {
// 토큰이 없으면 로그인 페이지 유지
return;
}
if (!token) return;
// 토큰이 있으면 API 호출로 유효성 확인
const result = await apiCall(AUTH_CONFIG.ENDPOINTS.STATUS);
const result = await apiCall<{ isAuthenticated?: boolean }>("GET", AUTH_CONFIG.ENDPOINTS.STATUS);
// 백엔드가 isAuthenticated 필드를 반환함
if (result.success && result.data?.isAuthenticated) {
// 이미 로그인된 경우 메인으로 리다이렉트
router.push(AUTH_CONFIG.ROUTES.MAIN);
} else {
// 토큰이 유효하지 않으면 제거
localStorage.removeItem("authToken");
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
}
} catch (error) {
// 에러가 발생하면 토큰 제거
} catch {
localStorage.removeItem("authToken");
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
console.debug("기존 인증 체크 중 오류 (정상):", error);
}
}, [apiCall, router]);
}, [router]);
/**
*
* (apiCall - Axios , fetch )
*/
const handleLogin = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
// 입력값 검증
const validationError = validateForm();
if (validationError) {
setError(validationError);
@ -144,9 +112,13 @@ export const useLogin = () => {
setError("");
try {
const result = await apiCall(AUTH_CONFIG.ENDPOINTS.LOGIN, {
method: "POST",
body: JSON.stringify(formData),
const result = await apiCall<{
token?: string;
firstMenuPath?: string;
popLandingPath?: string;
}>("POST", AUTH_CONFIG.ENDPOINTS.LOGIN, {
userId: formData.userId,
password: formData.password,
});
if (result.success && result.data?.token) {
@ -185,7 +157,7 @@ export const useLogin = () => {
setIsLoading(false);
}
},
[formData, validateForm, apiCall, router, isPopMode],
[formData, validateForm, router, isPopMode],
);
// 컴포넌트 마운트 시 기존 인증 상태 확인