"use client"; import { useState, useEffect } from "react"; import { Card, Button, Input } from "@/shared/components"; import { useRouter } from "next/navigation"; export default function LoginPage() { const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [hasPassword, setHasPassword] = useState(null); const router = useRouter(); // Check if password is set on mount useEffect(() => { async function checkPassword() { try { const res = await fetch("/api/settings"); if (res.ok) { const data = await res.json(); if (!data.password) { // No password set - auto login const loginRes = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password: "123456" }), }); if (loginRes.ok) { router.push("/dashboard"); router.refresh(); return; } } setHasPassword(!!data.password); } } catch (err) { console.error("Failed to check password status:", err); setHasPassword(true); // Default to showing login form } } checkPassword(); }, [router]); const handleLogin = async (e) => { e.preventDefault(); setLoading(true); setError(""); try { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }); if (res.ok) { router.push("/dashboard"); router.refresh(); } else { const data = await res.json(); setError(data.error || "Invalid password"); } } catch (err) { setError("An error occurred. Please try again."); } finally { setLoading(false); } }; // Show loading state while checking password if (hasPassword === null) { return (

Loading...

); } return (

9Router

Enter your password to access the dashboard

setPassword(e.target.value)} required autoFocus /> {error &&

{error}

}

Default password is 123456

); }