"use client"; import { useState, useId } from "react"; type LoginClientProps = { error: string | null; }; const REDIRECT_URL = "/admin"; // Development mode detection const isDev = process.env.NODE_ENV !== "production"; export default function LoginClient({ error }: LoginClientProps) { const emailId = useId(); const passwordId = useId(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [localError, setLocalError] = useState(null); async function handleSignIn() { if (!email || !password) { setLocalError("Email and password are required."); return; } setLocalError(null); setLoading(true); try { const res = await fetch("/api/auth/sign-in", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: email.trim().toLowerCase(), password: password.trim() }), }); const data = await res.json(); if (!res.ok || data.error) { setLocalError(data.message ?? "Sign-in failed. Please check your email and password."); setLoading(false); return; } // Cookie is set server-side via next/headers — just redirect on success. window.location.href = REDIRECT_URL; } catch { setLocalError("Sign-in failed. Please try again."); setLoading(false); } } function handleDevLogin(role: string) { // Set the dev_session cookie for local development bypass document.cookie = `dev_session=${role}; path=/; max-age=86400`; window.location.href = REDIRECT_URL; } const displayError = error ?? localError; return (
{/* Decorative grain layer */}
); }