import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useState } from "react"; import { useAuth, useLogin, useSetup } from "../hooks/useAuth"; export const Route = createFileRoute("/login")({ component: LoginPage, }); function LoginPage() { const navigate = useNavigate(); const { data: auth } = useAuth(); const login = useLogin(); const setup = useSetup(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const isSetup = auth?.setupRequired ?? false; async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); try { if (isSetup) { await setup.mutateAsync({ username, password }); } else { await login.mutateAsync({ username, password }); } navigate({ to: "/" }); } catch (err) { setError((err as Error).message); } } const isPending = login.isPending || setup.isPending; return (

{isSetup ? "Create Account" : "Sign In"}

{isSetup && (

Create your admin account to manage your gear collection.

)}
setUsername(e.target.value)} required className="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-200" />
setPassword(e.target.value)} required minLength={isSetup ? 6 : undefined} className="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-200" />
{error &&

{error}

}
); }