"use client"; import Image from "next/image"; import type React from "react"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { apiError, NONE_HTTP_ERROR } from "@kri/common/http"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { useLoginMutation } from "@/query/auth"; export default function LoginPage() { const router = useRouter(); const loginMutation = useLoginMutation(); const passwordInputRef = useRef(null); const [userId, setUserId] = useState(""); const [userPw, setUserPw] = useState(""); const [error, setError] = useState(null); const tryLogin = async () => { if (!userId.trim()) return window.alert("아이디를 입력해 주세요."); if (!userPw) return window.alert("비밀번호를 입력해 주세요."); setError(null); try { await loginMutation.mutateAsync({ userId: userId.trim(), userPw }); } catch (cause: unknown) { const errorInfo = apiError(cause, { fallbackMessage: "로그인에 실패했습니다." }); if (errorInfo.status === 401 || errorInfo.status === 403) setError("아이디 또는 비밀번호를 다시 확인해 주세요."); else if (errorInfo.code === NONE_HTTP_ERROR) setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해 주세요."); else setError(`로그인에 실패했습니다. (${errorInfo.message})`); } }; useEffect(() => { if (loginMutation.isSuccess) router.replace("/"); }, [loginMutation.isSuccess, router]); const handleIdKeyDown: React.KeyboardEventHandler = (event) => { if (event.key === "Enter") { event.preventDefault(); passwordInputRef.current?.focus(); } }; const handlePasswordKeyDown: React.KeyboardEventHandler = (event) => { if (event.key === "Enter") { event.preventDefault(); void tryLogin(); } }; return (
로고
setUserId(event.currentTarget.value)} onKeyDown={handleIdKeyDown} />
setUserPw(event.currentTarget.value)} onKeyDown={handlePasswordKeyDown} />
{error ?

{error}

: null}
); }