|
1 | 1 | "use client"; |
2 | | -import Input from "@/components/common/input/Input"; |
3 | | -import { AuthLayout } from "@/app/(before-login)/(without-navbar)/layout"; |
4 | 2 | import { useForm } from "react-hook-form"; |
5 | 3 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import Input from "@/components/common/input/Input"; |
| 5 | +import { AuthLayout } from "@/app/(before-login)/(without-navbar)/layout"; |
6 | 6 | import { loginSchema, LoginFormData } from "@/lib/utils/validationSchema"; |
| 7 | +import { useAlertStore } from "@/lib/store/useAlertStore"; |
| 8 | +import { fetchLogin } from "@/lib/apis/authApi"; |
| 9 | +import { useState } from "react"; |
| 10 | +import { useRouter } from "next/navigation"; |
7 | 11 |
|
8 | 12 | export default function Page() { |
| 13 | + const { openAlert } = useAlertStore(); |
| 14 | + const [isLoading, setIsLoading] = useState(false); |
| 15 | + const router = useRouter(); |
| 16 | + |
9 | 17 | const { |
10 | 18 | register, |
11 | 19 | handleSubmit, |
12 | | - formState: { errors }, |
| 20 | + formState: { errors, isValid }, |
13 | 21 | } = useForm<LoginFormData>({ |
14 | 22 | resolver: zodResolver(loginSchema), |
15 | 23 | mode: "onBlur", |
16 | 24 | }); |
17 | 25 |
|
18 | | - const onSubmit = (data: LoginFormData) => { |
19 | | - console.log("로그인 데이터:", data); |
| 26 | + const onSubmit = async (data: LoginFormData) => { |
| 27 | + setIsLoading(true); |
| 28 | + try { |
| 29 | + const response = await fetchLogin(data); |
| 30 | + setIsLoading(false); |
| 31 | + localStorage.setItem("accessToken", response.accessToken); |
| 32 | + openAlert("loginSuccess"); |
| 33 | + router.push("/mydashboard"); |
| 34 | + } catch (error: unknown) { |
| 35 | + setIsLoading(false); |
| 36 | + if (error instanceof Error) { |
| 37 | + const errorInfo: { status: number; message: string } = JSON.parse( |
| 38 | + error.message |
| 39 | + ); |
| 40 | + if (errorInfo.status === 400) { |
| 41 | + openAlert("wrongPassword"); |
| 42 | + } |
| 43 | + if (errorInfo.status === 404) { |
| 44 | + openAlert("userNotFound"); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
20 | 48 | }; |
| 49 | + |
21 | 50 | return ( |
22 | 51 | <AuthLayout |
23 | 52 | buttonText="로그인" |
24 | 53 | linkText="회원이 아니신가요?" |
25 | 54 | linkPath="/signup" |
| 55 | + isLoading={isLoading} |
| 56 | + isFormValid={isValid} |
26 | 57 | > |
27 | 58 | <form id="auth-form" onSubmit={handleSubmit(onSubmit)} className="w-full"> |
28 | 59 | <div className="pb-4"> |
|
0 commit comments