import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { cn } from "~/lib/utils"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "~/components/ui/form"; import { Input } from "~/components/ui/input"; import { useNavigate } from "react-router"; import { Links } from "~/lib/links"; import { authApi } from "~/api/auth-api.service"; const loginSchema = z.object({ input: z.string().email("Email không hợp lệ"), password: z.string().min(6, "Mật khẩu phải ít nhất 6 ký tự"), }); type LoginFormValues = z.infer; export function LoginForm({ className, ...props }: React.ComponentProps<"div">) { const navigate = useNavigate(); const form = useForm({ resolver: zodResolver(loginSchema), defaultValues: { input: "", password: "", }, }); const onSubmit = async (values: LoginFormValues) => { // Gọi API login const response = await authApi.login(values); // Nếu login thành công => reload lại trang if (response?.data) { navigate(Links.HOME, { replace: true, state: { from: Links.LOGIN } }); } }; return (
Login to your account Enter your email below to login to your account
( Email )} /> ( )} />
); }