From 5a49a56d0e0f815a3adac5ea52390ba854a51cfd Mon Sep 17 00:00:00 2001 From: Jungmin Date: Wed, 30 Oct 2024 21:28:34 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20React=20Query=EB=A1=9C=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B8=B0=EB=8A=A5=20=ED=86=B5=ED=95=A9=20?= =?UTF-8?q?=EB=B0=8F=20hooks=20=ED=8F=B4=EB=8D=94=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - hook ⇨ hooks로 폴더명 변경 - 로그인 로직을 React Query의 useMutation으로 리팩토링 - 로컬 로그인 오류 상태 관리 제거 - 로그인 실패 시 toast 알림을 통한 오류 처리 추가 - useSignup을 useAuth로 통합하여 인증 처리 통일 - LoginForm 컴포넌트를 새로운 useAuth 훅을 사용하도록 업데이트 --- src/app/(auth)/components/LoginForm.tsx | 26 +++------------- src/app/(auth)/components/SignupForm.tsx | 4 +-- src/app/dashboard/components/Sidebar.tsx | 2 +- src/app/dashboard/components/TodoCard.tsx | 2 +- src/app/dashboard/components/TodoItem.tsx | 2 +- src/components/CreateNewTodo.tsx | 2 +- src/hook/useSignup.tsx | 23 -------------- src/hooks/useAuth.tsx | 37 +++++++++++++++++++++++ src/{hook => hooks}/useModal.tsx | 0 9 files changed, 47 insertions(+), 51 deletions(-) delete mode 100644 src/hook/useSignup.tsx create mode 100644 src/hooks/useAuth.tsx rename src/{hook => hooks}/useModal.tsx (100%) diff --git a/src/app/(auth)/components/LoginForm.tsx b/src/app/(auth)/components/LoginForm.tsx index d115dd0..fd026c9 100644 --- a/src/app/(auth)/components/LoginForm.tsx +++ b/src/app/(auth)/components/LoginForm.tsx @@ -1,20 +1,16 @@ "use client"; -import { useState } from "react"; -import { useRouter } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; -import { AxiosError } from "axios"; -import { login } from "@/utils/authUtils"; import LoadingSpinner from "@/components/LoadingSpinner"; +import { useAuth } from "@/hooks/useAuth"; type FormData = z.infer; export default function LoginForm() { - const router = useRouter(); - const [loginError, setLoginError] = useState(null); + const { loginMutation } = useAuth(); const { register, @@ -26,22 +22,9 @@ export default function LoginForm() { resolver: zodResolver(schema), }); - const onSubmit = async (data: FormData) => { + const onSubmit = async (data: { email: string; password: string }) => { clearErrors(); - setLoginError(null); - - try { - const success = await login(data.email, data.password); - if (success) { - router.push("/dashboard"); - } else { - setLoginError("로그인에 실패했습니다. 다시 시도해 주세요."); - } - } catch (error) { - const axiosError = error as AxiosError; - console.error("로그인 중 오류 발생:", axiosError); - setLoginError("로그인 실패했습니다. 다시 시도해 주세요."); - } + loginMutation.mutate(data); }; return ( @@ -77,7 +60,6 @@ export default function LoginForm() { /> {errors.email && {errors.email.message}} {errors.password && {errors.password.message}} - {loginError && {loginError}}