"use client"; import { AnimatePresence, motion } from "motion/react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { Eye, EyeOff } from "lucide-react"; import { getAuthErrorDetails, isNetworkError, shouldRetry } from "@/lib/auth-errors"; export function LocalLoginForm() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(null); const [errorTitle, setErrorTitle] = useState(null); const [isLoading, setIsLoading] = useState(false); const [authType, setAuthType] = useState(null); const router = useRouter(); useEffect(() => { // Get the auth type from environment variables setAuthType(process.env.NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE || "GOOGLE"); }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(null); // Clear any previous errors setErrorTitle(null); // Show loading toast const loadingToast = toast.loading("Signing you in..."); try { // Create form data for the API request const formData = new URLSearchParams(); formData.append("username", username); formData.append("password", password); formData.append("grant_type", "password"); const response = await fetch( `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/jwt/login`, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: formData.toString(), } ); const data = await response.json(); if (!response.ok) { throw new Error(data.detail || `HTTP ${response.status}`); } // Success toast toast.success("Login successful!", { id: loadingToast, description: "Redirecting to dashboard...", duration: 2000, }); // Small delay to show success message setTimeout(() => { router.push(`/auth/callback?token=${data.access_token}`); }, 500); } catch (err) { // 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"; } // Get detailed error information from auth-errors utility const errorDetails = getAuthErrorDetails(errorCode); // Set persistent error display setErrorTitle(errorDetails.title); setError(errorDetails.description); // Show error toast with conditional retry action const toastOptions: any = { id: loadingToast, description: errorDetails.description, duration: 6000, }; // Add retry action if the error is retryable if (shouldRetry(errorCode)) { toastOptions.action = { label: "Retry", onClick: () => handleSubmit(e), }; } toast.error(errorDetails.title, toastOptions); } finally { setIsLoading(false); } }; return (
{/* Error Display */} {error && errorTitle && (
Error Icon

{errorTitle}

{error}

)}
setUsername(e.target.value)} className={`mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:bg-gray-800 dark:text-white transition-colors ${ error ? "border-red-300 focus:border-red-500 focus:ring-red-500 dark:border-red-700" : "border-gray-300 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-700" }`} disabled={isLoading} />
setPassword(e.target.value)} className={`mt-1 block w-full rounded-md border pr-10 px-3 py-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:bg-gray-800 dark:text-white transition-colors ${ error ? "border-red-300 focus:border-red-500 focus:ring-red-500 dark:border-red-700" : "border-gray-300 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-700" }`} disabled={isLoading} />
{authType === "LOCAL" && (

Don't have an account?{" "} Register here

)}
); }