-
-
-
-
+
+
+
-
-
+
);
};
-
export default HomePage;
diff --git a/src/pages/LoginPage.tsx b/src/pages/LoginPage.tsx
index 335eb44c..b81b5d2e 100644
--- a/src/pages/LoginPage.tsx
+++ b/src/pages/LoginPage.tsx
@@ -1,116 +1,124 @@
-import React, { useEffect } from "react";
-import useLoginForm from "../hooks/useLoginForm";
-import { useAuth } from "../context/AuthContext";
-import { useNavigate, useLocation } from "react-router-dom";
-import { PATHS } from "../constants/paths";
+import { useState, useEffect } from "react";
+import { useForm, type SubmitHandler } from "react-hook-form";
+import { useAuth } from "../contexts/useAuth";
+import { useNavigate } from "react-router-dom";
+
+interface LoginFormInputs {
+ email: string;
+ password: string;
+}
const LoginPage = () => {
const { login, isLoggedIn } = useAuth();
const navigate = useNavigate();
- const location = useLocation();
const {
- email,
- password,
- emailError,
- passwordError,
- handleEmailChange,
- handleEmailBlur,
- handlePasswordChange,
- handlePasswordBlur,
- isFormValid,
- } = useLoginForm();
+ register,
+ handleSubmit,
+ formState: { errors, isValid, isSubmitting },
+ } = useForm
({
+ mode: "onChange",
+ defaultValues: {
+ email: "",
+ password: "",
+ },
+ });
+
+ const [loginError, setLoginError] = useState(null);
useEffect(() => {
if (isLoggedIn) {
- const from = location.state?.from?.pathname || PATHS.MY_GIFTS;
- navigate(from, { replace: true });
+ navigate("/");
}
- }, [isLoggedIn, navigate, location.state]);
-
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault();
-
- handleEmailBlur();
- handlePasswordBlur();
-
- if (isFormValid) {
- const isEmailFormatValid = !emailError;
- const isPasswordLengthValid = password.length >= 8;
+ }, [isLoggedIn, navigate]);
- const simulateLoginSuccess = isEmailFormatValid && isPasswordLengthValid;
+ const onSubmit: SubmitHandler = async (data) => {
+ setLoginError(null);
- if (simulateLoginSuccess) {
- alert(`로그인 성공! 이메일: ${email}`);
- console.log("로그인 성공!");
- login(email ?? "");
+ try {
+ await login(data.email, data.password);
+ navigate("/");
+ } catch (err) {
+ if (err instanceof Error) {
+ setLoginError(err.message);
} else {
- alert(
- "로그인 실패: 이메일 형식을 확인하거나 비밀번호를 8자리 이상 입력해주세요."
- );
- console.log("로그인 실패: 테스트 조건 불충족.");
+ setLoginError("로그인 중 알 수 없는 오류가 발생했습니다.");
}
- } else {
- console.log("유효성 검사 실패. 모든 필드를 올바르게 입력해주세요.");
-
- alert("입력 양식을 올바르게 작성해주세요.");
}
};
- if (isLoggedIn) {
- return null;
- }
-
return (