"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { toast, Toaster } from "sonner"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { useAuth } from "@/lib/auth-context"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; const formSchema = z.object({ email: z.string().email({ message: "Invalid email address" }), password: z .string() .min(6, { message: "Password must be at least 6 characters" }), }); export function LoginForm() { const router = useRouter(); const searchParams = new URLSearchParams( typeof window !== "undefined" ? window.location.search : "" ); const redirectPath = searchParams.get("redirect") || "/"; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { email: "", password: "", }, }); const { login } = useAuth(); const onSubmit = async (data: z.infer) => { try { const response = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }); const result = await response.json(); if (!response.ok) { toast.error(result.error || "Login failed"); return; } // Use the auth context to handle login login(result.token, { ...result.user, FirstName: result.user.FirstName, LastName: result.user.LastName, Email: result.user.Email, }); toast.success("Login successful!"); // Redirect to the original destination or default route router.push(redirectPath); } catch (error) { console.error("Login error:", error); toast.error("Connection error. Please try again."); } }; return (
( Email )} /> ( Password )} />
); }