136 lines
5.0 KiB
TypeScript
136 lines
5.0 KiB
TypeScript
"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 {useRouter} from "next/navigation";
|
|
import {apiError} from "@kri/common/http";
|
|
import {NONE_HTTP_ERROR} from "@kri/common/http";
|
|
import {useLoginMutation} from "@/query/auth";
|
|
|
|
const LoginPage = () => {
|
|
const router = useRouter();
|
|
|
|
const loginMutation = useLoginMutation();
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const [userId, setUserId] = useState<string>("");
|
|
const [userPw, setUserPw] = useState<string>("");
|
|
|
|
const passwordInputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
const tryLogin = async () => {
|
|
if (!userId) return alert("Please enter ID.");
|
|
if (!userPw) return alert("Please enter password.");
|
|
|
|
setError(null);
|
|
try {
|
|
await loginMutation.mutateAsync({ userId, userPw });
|
|
} catch (e) {
|
|
const err = apiError(e, {
|
|
fallbackMessage: "Login failed.",
|
|
});
|
|
if (err.status === 401 || err.status === 403) setError("아이디 또는 비밀번호를 다시 확인해주세요.");
|
|
else if (err.code === NONE_HTTP_ERROR) setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해주세요.");
|
|
else setError(`로그인에 실패했습니다. (${err.message})`);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (loginMutation.isSuccess) {
|
|
router.replace("/");
|
|
}
|
|
}, [loginMutation.isSuccess, router]);
|
|
|
|
const handleIdKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
passwordInputRef.current?.focus();
|
|
}
|
|
};
|
|
|
|
const handlePwKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
tryLogin();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 p-4">
|
|
<Card className="w-full max-w-md shadow-xl">
|
|
<CardHeader className="space-y-4 pb-8">
|
|
<div className="flex justify-center">
|
|
<div className="w-60 h-32 flex items-center justify-center">
|
|
<Image
|
|
src="/kri_logo.png" // public/kri_logo.png
|
|
alt="KRI 로고"
|
|
width={500}
|
|
height={500}
|
|
className="object-contain ml-10"
|
|
priority
|
|
/>
|
|
</div>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="space-y-4">
|
|
<div className="space-y-2">
|
|
<label className="text-base font-medium">ID</label>
|
|
<Input
|
|
id="id"
|
|
name="id"
|
|
type="text"
|
|
required
|
|
placeholder="ID"
|
|
value={userId}
|
|
onChange={(e) => setUserId(e.currentTarget.value)}
|
|
onKeyDown={handleIdKeyDown}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<label className="text-base font-medium">Password</label>
|
|
<Input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
placeholder="password"
|
|
value={userPw}
|
|
onChange={(e) => setUserPw(e.currentTarget.value)}
|
|
onKeyDown={handlePwKeyDown}
|
|
ref={passwordInputRef}
|
|
/>
|
|
</div>
|
|
|
|
{error && (
|
|
<p className="text-sm text-red-500">
|
|
{error}
|
|
</p>
|
|
)}
|
|
|
|
<Button
|
|
type="submit"
|
|
className="w-full mt-6 h-11"
|
|
disabled={loginMutation.isPending}
|
|
onClick={(e) => {
|
|
e.preventDefault();
|
|
tryLogin();
|
|
}}
|
|
>
|
|
로그인
|
|
</Button>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default LoginPage;
|