"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 { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { authClient, signIn, signUp } from "@/lib/auth/client"; export interface AuthConfig { oidcEnabled: boolean; oidcProviderName: string | null; passwordLoginDisabled: boolean; registrationOpen?: boolean; } const authInputClass = "h-11 rounded-lg border-border/50 bg-background/50 px-4 py-0 placeholder:text-muted-foreground/50 focus-visible:border-primary/40 focus-visible:ring-ring md:text-sm"; 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 && ( )} {showOidc && showPasswordForm && (
or
)} {showPasswordForm && ( {isRegister && ( setName(e.target.value)} className={authInputClass} placeholder="Your name…" /> )} setEmail(e.target.value)} className={authInputClass} placeholder="wwhite@graymatter.biz" /> setPassword(e.target.value)} className={authInputClass} placeholder="Min 8 characters…" /> )} {error && ( {error} )} {showPasswordForm && (isRegister || authConfig?.registrationOpen !== false) && (

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

)}
); }