"use client" import * as React from "react" import { useSearchParams } from "next/navigation" import { signIn } from "next-auth/react" import * as z from "zod" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { cn } from "@/lib/utils" import { userAuthSchema } from "@/lib/validations/auth" import { toast } from "@/ui/toast" import { Icons } from "@/components/icons" interface UserAuthFormProps extends React.HTMLAttributes {} type FormData = z.infer export function UserAuthForm({ className, ...props }: UserAuthFormProps) { const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(userAuthSchema), }) const [isLoading, setIsLoading] = React.useState(false) const searchParams = useSearchParams() async function onSubmit(data: FormData) { setIsLoading(true) const signInResult = await signIn("email", { email: data.email.toLowerCase(), redirect: false, callbackUrl: searchParams.get("from") || "/dashboard", }) setIsLoading(false) if (!signInResult?.ok) { return toast({ title: "Something went wrong.", message: "Your post was not saved. Please try again.", type: "error", }) } return toast({ title: "Check your email", message: "We sent you a login link. Be sure to check your spam too.", type: "success", }) } return (
{errors?.email && (

{errors.email.message}

)}
Or continue with
) }