"use client"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Mail, Loader2, CheckCircle } from "lucide-react"; import { toast } from "sonner"; import { authClient } from "@/lib/auth-client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; const formSchema = z.object({ email: z.string().email("Please enter a valid email address"), }); type FormData = z.infer; export function MagicLinkForm() { const [isLoading, setIsLoading] = useState(false); const [emailSent, setEmailSent] = useState(false); const [sentEmail, setSentEmail] = useState(""); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { email: "", }, }); async function onSubmit(data: FormData) { setIsLoading(true); try { const result = await authClient.signIn.magicLink({ email: data.email, callbackURL: "/", }); if (result.error) { toast.error(result.error.message || "Failed to send magic link"); return; } setEmailSent(true); setSentEmail(data.email); toast.success("Magic link sent! Check your email."); } catch (error) { toast.error("An unexpected error occurred"); console.error("Magic link error:", error); } finally { setIsLoading(false); } } if (emailSent) { return (

Check your email

We sent a magic link to {sentEmail}

Click the link in the email to sign in. The link expires in 5 minutes.

); } return (
( Email
)} /> ); }