260821_01_FST: First Commit
This commit is contained in:
81
src/app/(pages)/login/page.tsx
Normal file
81
src/app/(pages)/login/page.tsx
Normal file
@@ -0,0 +1,81 @@
|
||||
"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<HTMLInputElement | null>(null);
|
||||
const [userId, setUserId] = useState("");
|
||||
const [userPw, setUserPw] = useState("");
|
||||
const [error, setError] = useState<string | null>(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<HTMLInputElement> = (event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
passwordInputRef.current?.focus();
|
||||
}
|
||||
};
|
||||
|
||||
const handlePasswordKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
void tryLogin();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<main className="flex min-h-screen items-center justify-center bg-muted/40 p-4">
|
||||
<Card className="w-full max-w-md shadow-xl">
|
||||
<CardHeader className="pb-8">
|
||||
<div className="flex justify-center">
|
||||
<Image src="/logo.png" alt="로고" width={240} height={120} className="h-24 w-auto object-contain" priority />
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="userId" className="text-sm font-medium">ID</label>
|
||||
<Input id="userId" autoComplete="username" placeholder="아이디를 입력하세요" value={userId} onChange={(event) => setUserId(event.currentTarget.value)} onKeyDown={handleIdKeyDown} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="userPw" className="text-sm font-medium">Password</label>
|
||||
<Input ref={passwordInputRef} id="userPw" type="password" autoComplete="current-password" placeholder="비밀번호를 입력하세요" value={userPw} onChange={(event) => setUserPw(event.currentTarget.value)} onKeyDown={handlePasswordKeyDown} />
|
||||
</div>
|
||||
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
||||
<Button className="mt-6 h-11 w-full" disabled={loginMutation.isPending} onClick={() => void tryLogin()}>
|
||||
{loginMutation.isPending ? "로그인 중..." : "로그인"}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user