[agent-pipeline] pipe-20260317084014-ydap round-3
This commit is contained in:
parent
ad48b22770
commit
cfd7ee9fce
|
|
@ -2,12 +2,13 @@
|
||||||
|
|
||||||
import { useState, useEffect, useCallback } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
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 { 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 = () => {
|
export const useLogin = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
@ -73,67 +74,34 @@ export const useLogin = () => {
|
||||||
}, [formData]);
|
}, [formData]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API 호출 공통 함수
|
* 기존 인증 상태 확인 (apiCall 사용)
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 기존 인증 상태 확인
|
|
||||||
*/
|
*/
|
||||||
const checkExistingAuth = useCallback(async () => {
|
const checkExistingAuth = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
// 로컬 스토리지에서 토큰 확인
|
|
||||||
const token = localStorage.getItem("authToken");
|
const token = localStorage.getItem("authToken");
|
||||||
if (!token) {
|
if (!token) return;
|
||||||
// 토큰이 없으면 로그인 페이지 유지
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 토큰이 있으면 API 호출로 유효성 확인
|
const result = await apiCall<{ isAuthenticated?: boolean }>("GET", AUTH_CONFIG.ENDPOINTS.STATUS);
|
||||||
const result = await apiCall(AUTH_CONFIG.ENDPOINTS.STATUS);
|
|
||||||
|
|
||||||
// 백엔드가 isAuthenticated 필드를 반환함
|
|
||||||
if (result.success && result.data?.isAuthenticated) {
|
if (result.success && result.data?.isAuthenticated) {
|
||||||
// 이미 로그인된 경우 메인으로 리다이렉트
|
|
||||||
router.push(AUTH_CONFIG.ROUTES.MAIN);
|
router.push(AUTH_CONFIG.ROUTES.MAIN);
|
||||||
} else {
|
} else {
|
||||||
// 토큰이 유효하지 않으면 제거
|
|
||||||
localStorage.removeItem("authToken");
|
localStorage.removeItem("authToken");
|
||||||
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
|
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
// 에러가 발생하면 토큰 제거
|
|
||||||
localStorage.removeItem("authToken");
|
localStorage.removeItem("authToken");
|
||||||
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
|
document.cookie = "authToken=; path=/; max-age=0; SameSite=Lax";
|
||||||
console.debug("기존 인증 체크 중 오류 (정상):", error);
|
|
||||||
}
|
}
|
||||||
}, [apiCall, router]);
|
}, [router]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 로그인 처리
|
* 로그인 처리 (apiCall 사용 - Axios 기반, fetch 미사용)
|
||||||
*/
|
*/
|
||||||
const handleLogin = useCallback(
|
const handleLogin = useCallback(
|
||||||
async (e: React.FormEvent) => {
|
async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
// 입력값 검증
|
|
||||||
const validationError = validateForm();
|
const validationError = validateForm();
|
||||||
if (validationError) {
|
if (validationError) {
|
||||||
setError(validationError);
|
setError(validationError);
|
||||||
|
|
@ -144,9 +112,13 @@ export const useLogin = () => {
|
||||||
setError("");
|
setError("");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await apiCall(AUTH_CONFIG.ENDPOINTS.LOGIN, {
|
const result = await apiCall<{
|
||||||
method: "POST",
|
token?: string;
|
||||||
body: JSON.stringify(formData),
|
firstMenuPath?: string;
|
||||||
|
popLandingPath?: string;
|
||||||
|
}>("POST", AUTH_CONFIG.ENDPOINTS.LOGIN, {
|
||||||
|
userId: formData.userId,
|
||||||
|
password: formData.password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result.success && result.data?.token) {
|
if (result.success && result.data?.token) {
|
||||||
|
|
@ -185,7 +157,7 @@ export const useLogin = () => {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[formData, validateForm, apiCall, router, isPopMode],
|
[formData, validateForm, router, isPopMode],
|
||||||
);
|
);
|
||||||
|
|
||||||
// 컴포넌트 마운트 시 기존 인증 상태 확인
|
// 컴포넌트 마운트 시 기존 인증 상태 확인
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue