"use client"; import { IconKey } from "@tabler/icons-react"; import { AnimatePresence, motion } from "motion/react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { SofaLogo } from "@/components/sofa-logo"; import { authClient, signIn, signUp } from "@/lib/auth/client"; export interface AuthConfig { oidcEnabled: boolean; oidcProviderName: string | null; passwordLoginDisabled: boolean; } const fieldVariants = { hidden: { opacity: 0, y: 10 }, visible: { opacity: 1, y: 0, transition: { type: "spring" as const, stiffness: 300, damping: 24 }, }, }; export function AuthForm({ mode, authConfig, }: { mode: "login" | "register"; authConfig?: AuthConfig; }) { const router = useRouter(); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [oidcLoading, setOidcLoading] = useState(false); const isRegister = mode === "register"; const showOidc = authConfig?.oidcEnabled ?? false; const showPasswordForm = !(authConfig?.passwordLoginDisabled ?? false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); setLoading(true); try { if (isRegister) { const result = await signUp.email({ name, email, password }); if (result.error) { setError(result.error.message ?? "Registration failed"); return; } } else { const result = await signIn.email({ email, password }); if (result.error) { setError(result.error.message ?? "Login failed"); return; } } router.push("/dashboard"); router.refresh(); } catch { setError("Something went wrong"); } finally { setLoading(false); } } async function handleOidcLogin() { setError(""); setOidcLoading(true); try { await authClient.signIn.oauth2({ providerId: "oidc", callbackURL: "/dashboard", }); } catch { setError("Failed to start SSO login"); setOidcLoading(false); } } return (
{/* Subtle glow behind card */}

{isRegister ? "Create your account" : "Welcome back"}

{isRegister ? "Start tracking your watches" : "Sign in to continue"}

{showOidc && ( {oidcLoading ? "Redirecting..." : `Sign in with ${authConfig?.oidcProviderName || "SSO"}`} )} {showOidc && showPasswordForm && (
or
)} {showPasswordForm && ( {isRegister && ( setName(e.target.value)} className="flex h-11 w-full rounded-lg border border-border/50 bg-background/50 px-4 text-sm transition-colors placeholder:text-muted-foreground/50 focus:border-primary/40 focus:outline-none focus:ring-1 focus:ring-primary/20" placeholder="Your name" /> )} setEmail(e.target.value)} className="flex h-11 w-full rounded-lg border border-border/50 bg-background/50 px-4 text-sm transition-colors placeholder:text-muted-foreground/50 focus:border-primary/40 focus:outline-none focus:ring-1 focus:ring-primary/20" placeholder="wwhite@graymatter.biz" /> setPassword(e.target.value)} className="flex h-11 w-full rounded-lg border border-border/50 bg-background/50 px-4 text-sm transition-colors placeholder:text-muted-foreground/50 focus:border-primary/40 focus:outline-none focus:ring-1 focus:ring-primary/20" placeholder="Min 8 characters" /> {loading ? "Loading..." : isRegister ? "Create account" : "Sign in"} )} {error && ( {error} )} {showPasswordForm && (

{isRegister ? ( <> Already have an account?{" "} Sign in ) : ( <> Don't have an account?{" "} Register )}

)}
); }