"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 (
{isRegister ? "Start tracking your watches" : "Sign in to continue"}
{isRegister ? ( <> Already have an account?{" "} Sign in > ) : ( <> Don't have an account?{" "} Register > )}
)}