"use client"; import * as React from "react"; import Link from "next/link"; 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 } from "./auth-card"; import { PasswordInput } from "./password-input"; import { PasswordStrength, scorePassword } from "./password-strength"; import { ResendVerification, RESEND_COOLDOWN_S } from "./resend-verification"; import { humanAuthError, isValidEmail, passwordLengthError, type AuthClientError } from "./auth-errors"; export function SignupForm() { const [name, setName] = React.useState(""); 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<{ name?: string; email?: string; password?: string }>({}); const [sentTo, setSentTo] = React.useState(null); async function onSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); const fe: typeof fieldErrors = {}; if (name.trim().length < 1) fe.name = "Tell us what to call you."; if (name.trim().length > 80) fe.name = "Keep it under 80 characters."; if (!isValidEmail(email)) fe.email = "Enter a valid email address."; const pwErr = passwordLengthError(password); if (pwErr) fe.password = pwErr; else if (scorePassword(password).score < 2) fe.password = "That password is too easy to guess. Try a longer or less common one."; setFieldErrors(fe); if (Object.keys(fe).length) return; setLoading(true); try { const { error: err } = await authClient.signUp.email({ name: name.trim(), email: email.trim(), password, callbackURL: "/verify-email" }); if (err) { setError(humanAuthError(err as AuthClientError, "signup")); return; } toast.success("Account created", "Check your inbox to verify your email."); setSentTo(email.trim()); } catch { setError("We couldn't reach the server. Check your connection and try again."); } finally { setLoading(false); } } if (sentTo) { return ( } title="Check your inbox" description={ <> We sent a verification link to {sentTo}. Open it to activate your account — the link is valid for 24 hours. } footer={ <> Wrong address?{" "} } >

Nothing after a minute? Check your spam folder or try resending.

); } return ( Already have an account?{" "} Sign in } >
{error} setName(e.target.value)} placeholder="Ada Lovelace" aria-invalid={!!fieldErrors.name || undefined} autoFocus /> setEmail(e.target.value)} placeholder="you@company.com" aria-invalid={!!fieldErrors.email || undefined} /> setPassword(e.target.value)} placeholder="10 to 128 characters" invalid={!!fieldErrors.password} aria-describedby="signup-password-strength" />

By creating an account you agree to the{" "} Terms {" "} and{" "} Privacy Policy .

); }