"use client"; import * as React from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { MailCheck } from "lucide-react"; import { authClient } from "@/lib/auth-client"; import { Button } from "@/components/ui/button"; import { Field, Input } from "@/components/ui/input"; import { toast } from "@/components/ui/toast"; import { AuthCard, FormError, FormNotice } from "./auth-card"; import { PasswordInput } from "./password-input"; import { ResendVerification, RESEND_COOLDOWN_S } from "./resend-verification"; import { humanAuthError, isValidEmail, type AuthClientError } from "./auth-errors"; export function LoginForm({ next, notice }: { next: string; notice?: "reset" | "verified" | "signed-out" }) { const router = useRouter(); const [email, setEmail] = React.useState(""); const [password, setPassword] = React.useState(""); const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const [fieldErrors, setFieldErrors] = React.useState<{ email?: string; password?: string }>({}); const [unverified, setUnverified] = React.useState(false); async function onSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setUnverified(false); const fe: typeof fieldErrors = {}; if (!isValidEmail(email)) fe.email = "Enter a valid email address."; if (!password) fe.password = "Enter your password."; setFieldErrors(fe); if (Object.keys(fe).length) return; setLoading(true); try { const { error: err } = await authClient.signIn.email({ email: email.trim(), password, callbackURL: "/verify-email" }); if (err) { const ae = err as AuthClientError; if (ae.status === 403 || ae.code === "EMAIL_NOT_VERIFIED") { setUnverified(true); return; } setError(humanAuthError(ae, "login")); return; } toast.success("Welcome back"); router.push(next); router.refresh(); } catch { setError("We couldn't reach the server. Check your connection and try again."); } finally { setLoading(false); } } if (unverified) { return ( } title="Verify your email first" description={ <> Your account exists but {email.trim()} hasn't been verified yet. We just sent you a fresh link — open it to finish signing in. } footer={ } >

Check your spam folder if nothing arrives within a minute.

); } return ( New to PolyLLM?{" "} Create an account } >
{notice === "reset" ? Your password was updated. Sign in with the new one. : null} {notice === "verified" ? Email verified. You can sign in now. : null} {notice === "signed-out" ? You have been signed out. : null} {error} setEmail(e.target.value)} placeholder="you@company.com" aria-invalid={!!fieldErrors.email || undefined} aria-describedby={error ? "login-error" : undefined} autoFocus /> setPassword(e.target.value)} placeholder="Your password" invalid={!!fieldErrors.password} />
Forgot your password?
); }