"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; export function LocalLoginForm() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); 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(""); 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 || "Failed to login"); } router.push(`/auth/callback?token=${data.access_token}`); } catch (err) { const errorMessage = err instanceof Error ? err.message : "An error occurred during login"; setError(errorMessage); } finally { setIsLoading(false); } }; return (
{error && (
{error}
)}
setUsername(e.target.value)} className="mt-1 block w-full rounded-md border border-gray-300 bg-white px-3 py-2 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-blue-500 dark:border-gray-700 dark:bg-gray-800 dark:text-white" />
setPassword(e.target.value)} className="mt-1 block w-full rounded-md border border-gray-300 bg-white px-3 py-2 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-blue-500 dark:border-gray-700 dark:bg-gray-800 dark:text-white" />
{authType === "LOCAL" && (

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

)}
); }