"use client"; import Image from "next/image"; import {Button} from "@/components/ui/button"; import {Card, CardContent, CardHeader} from "@/components/ui/card"; import {Input} from "@/components/ui/input"; import React, {useEffect, useRef, useState} from "react"; import { nextApi } from "@/lib/http-client"; import {LoginUser} from "@/types/auth"; import { useAuth } from "@/hooks/useAuth"; import { useLoading } from "@/hooks/useLoading"; import {apiError} from "@/lib/http-util"; import {NONE_HTTP_ERROR} from "@/const/commonConst"; const LoginPage = () => { const { auth, setFromLoginResponse } = useAuth(); const { withLoadingAsync } = useLoading(); const [error, setError] = useState(null); const [userId, setUserId] = useState(""); const [userPw, setUserPw] = useState(""); const passwordInputRef = useRef(null); const tryLogin = () => { if(!userId){ alert("아이디를 입력해 주세요"); return; } if(!userPw){ alert("PW를 입력해 주세요"); return; } setError(null); const loginRequestBody = {userId, userPw}; const loginRequestOptions = {autoLogoutOnAuthError: false}; // withLoading 래퍼로 API 호출 전/후 로딩 표시를 자동으로 처리 return withLoadingAsync(() => nextApi.post( "/login", loginRequestBody, loginRequestOptions ).then(async (data) => { // Jotai 기반 전역 인증 상태 갱신 setFromLoginResponse(data); }).catch((e) => { const err = apiError(e); console.error(err.status, err.code, err.message); if (err.status === 401 || err.status === 403) { setError("아이디 또는 비밀번호를 다시 확인해주세요."); } else if(err.code === NONE_HTTP_ERROR) { setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해주세요."); } else { setError(`로그인에 실패했습니다. (status: ${err.status})`); } }) ); } useEffect(() => { if(auth.user){ window.location.replace("/"); } }, [auth.user]); const handleIdKeyDown: React.KeyboardEventHandler = (e) => { if (e.key === "Enter") { e.preventDefault(); passwordInputRef.current?.focus(); } }; const handlePwKeyDown: React.KeyboardEventHandler = (e) => { if (e.key === "Enter") { e.preventDefault(); tryLogin(); } }; const handleSeatNumberKeyDown: React.KeyboardEventHandler = (e) => { if (e.key === "Enter") { e.preventDefault(); tryLogin(); } }; return (
브랜드 로고
LG 보급률 조사 Dashboard
setUserId(e.currentTarget.value)} onKeyDown={handleIdKeyDown} />
setUserPw(e.currentTarget.value)} onKeyDown={handlePwKeyDown} ref={passwordInputRef} />
{error && (

{error}

)}
); } export default LoginPage;