"use client"; import React from "react"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Link } from "@/i18n/routing"; import { Icon } from "@/components/ui/icon"; import { useForm, SubmitHandler } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { cn } from "@/lib/utils"; import { Loader2 } from "lucide-react"; import { loginUser } from "@/action/auth-action"; import { toast } from "sonner"; import { useRouter } from "@/components/navigation"; const schema = z.object({ email: z.string().email({ message: "Your email is invalid." }), password: z.string().min(4), }); const LoginForm = () => { const [isPending, startTransition] = React.useTransition(); const router = useRouter(); const [passwordType, setPasswordType] = React.useState("password"); const togglePasswordType = () => { if (passwordType === "text") { setPasswordType("password"); } else if (passwordType === "password") { setPasswordType("text"); } }; const { register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: zodResolver(schema), mode: "all", defaultValues: { email: "dashcode@codeshaper.net", password: "password", }, }); const [isVisible, setIsVisible] = React.useState(false); const toggleVisibility = () => setIsVisible(!isVisible); const onSubmit = (data: z.infer) => { startTransition(async () => { try { const response = await loginUser(data); if (!!response.error) { toast("Event has been created", { description: "Sunday, December 03, 2023 at 9:00 AM", }); } else { router.push("/dashboard"); toast.success("Successfully logged in"); } } catch (err: any) { toast.error(err.message); } }); }; return (
{errors.email && (
{errors.email.message}
)}
{passwordType === "password" ? ( ) : ( )}
{errors.password && (
{errors.password.message}
)}
Forgot Password?
); }; export default LoginForm;