"use client"; import { signIn } from "next-auth/react"; import { useRouter, useSearchParams } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; const ERROR_MESSAGES: Record = { CredentialsSignin: "Invalid username or password.", SessionRequired: "Please sign in to continue.", Verification: "Sign-in link is invalid or expired.", AccessDenied: "You don't have access.", Configuration: "Auth configuration error — contact the server admin.", }; function LoginInner() { const router = useRouter(); const params = useSearchParams(); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); useEffect(() => { const qErr = params.get("error"); if (qErr) setError(ERROR_MESSAGES[qErr] || `Sign-in failed (${qErr}).`); }, [params]); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(""); const formData = new FormData(e.currentTarget); const res = await signIn("credentials", { username: formData.get("username"), password: formData.get("password"), redirect: false, }); if (res?.error) { setError(ERROR_MESSAGES[res.error] || "Invalid credentials."); setLoading(false); } else { const callback = params.get("callbackUrl") || "/admin"; router.push(callback); } } return (
Admin Login
{error && (
{error}
)}
); } export default function LoginPage() { return ( ); }