/** * @file LoginPage.tsx * @description Client component providing a user login interface with NextAuth credentials authentication. */ "use client"; import { useState } from "react"; import { signIn } from "next-auth/react"; import { useRouter } from "next/navigation"; import Link from "next/link"; /** * Renders the login page containing the authentication form, error handling, * and navigation links for user sign-in. * * @returns {JSX.Element} The rendered login page component. */ export default function LoginPage() { const router = useRouter(); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); /** * Handles the submission of the login form, validates credentials via NextAuth, * and manages loading/error states or redirects upon success. * * @async * @param {React.SubmitEvent} e - The form submission event. * @returns {Promise} */ const handleSubmit = async (e: React.SubmitEvent) => { e.preventDefault(); setError(null); setLoading(true); const formData = new FormData(e.currentTarget); const email = formData.get("email") as string; const password = formData.get("password") as string; const defaultErrorMessage = "An unexpected error has occurred."; try { // Attempt to sign in using Auth.js credentials provider const res = await signIn("credentials", { email, password, redirect: false, }); if (res?.error) { // Handle specific authentication errors if (res.error === "CredentialsSignin") { setError("Incorrect email address or password."); } else { setError(defaultErrorMessage); } } else { // Redirect user router.push("/summary"); return; } } catch (err: any) { // Handle network or connection errors vs unexpected errors if (err?.message?.includes("fetch") || err?.name === "TypeError") { setError("Server error: API endpoint not available"); } else { setError(defaultErrorMessage); } } finally { setLoading(false); } }; return (
{/* Login Card Container */}
{/* Header Section */}

Flowstate Login

Sign in to continue.

{/* Error Notification Banner */} {error && (
{error}
)} {/* Credentials Form */}
{/* Registration Link Footer */}
Don't have an account yet?{" "} Register
); }