"use client"; import { useAtom } from "jotai"; import { Eye, EyeOff } from "lucide-react"; import { AnimatePresence, motion } from "motion/react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { loginMutationAtom } from "@/atoms/auth/auth-mutation.atoms"; import { Spinner } from "@/components/ui/spinner"; import { getAuthErrorDetails, isNetworkError } from "@/lib/auth-errors"; import { AUTH_TYPE } from "@/lib/env-config"; import { ValidationError } from "@/lib/error"; import { trackLoginAttempt, trackLoginFailure, trackLoginSuccess } from "@/lib/posthog/events"; export function LocalLoginForm() { const t = useTranslations("auth"); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState<{ title: string | null; message: string | null; }>({ title: null, message: null, }); const authType = AUTH_TYPE; const router = useRouter(); const [{ mutateAsync: login, isPending: isLoggingIn }] = useAtom(loginMutationAtom); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError({ title: null, message: null }); // Clear any previous errors // Track login attempt trackLoginAttempt("local"); try { const data = await login({ username, password, grant_type: "password", }); // Track successful login trackLoginSuccess("local"); // Set flag so TokenHandler knows local login was already tracked if (typeof window !== "undefined") { sessionStorage.setItem("login_success_tracked", "true"); } // Small delay to show success message setTimeout(() => { router.push(`/auth/callback?token=${data.access_token}`); }, 500); } catch (err) { if (err instanceof ValidationError) { trackLoginFailure("local", err.message); setError({ title: err.name, message: err.message }); return; } // Use auth-errors utility to get proper error details let errorCode = "UNKNOWN_ERROR"; if (err instanceof Error) { errorCode = err.message; } else if (isNetworkError(err)) { errorCode = "NETWORK_ERROR"; } // Track login failure trackLoginFailure("local", errorCode); // Get detailed error information from auth-errors utility const errorDetails = getAuthErrorDetails(errorCode); // Set persistent error display setError({ title: errorDetails.title, message: errorDetails.description, }); } }; return (
{t("dont_have_account")}{" "} {t("sign_up")}