/** * @file (auth)/login/page.tsx * @description Client component providing a user login interface with NextAuth credentials authentication and guest login capabilities. */ "use client"; import { useState } from "react"; import { signIn } from "next-auth/react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { getGuestCredentials } from "@/services/auth.service"; /** * 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 [loadingType, setLoadingType] = useState< "credentials" | "guest" | null >(null); /** * Universal sign-in handler for both manual credentials and guest login. * * @async * @param {string} email - The user email. * @param {string} password - The user password. * @param {"credentials" | "guest"} type - The login method type for specific loading states. * @returns {Promise} */ const handleSignIn = async ( email: string, password: string, type: "credentials" | "guest", ) => { setError(null); setLoadingType(type); const defaultErrorMessage = "An unexpected error has occurred."; try { const res = await signIn("credentials", { email, password, redirect: false, }); if (res?.error) { if (res.error === "CredentialsSignin") { setError("Incorrect email address or password."); } else { setError(defaultErrorMessage); } } else { router.push("/summary"); return; } } catch (err: any) { if (err?.message?.includes("fetch") || err?.name === "TypeError") { setError("Server error: API endpoint not available"); } else { setError(defaultErrorMessage); } } finally { setLoadingType(null); } }; /** * Handles regular form submission. * * @async * @param {React.SubmitEvent} e - The form submission event. * @returns {Promise} */ const handleSubmit = async (e: React.SubmitEvent) => { e.preventDefault(); const formData = new FormData(e.currentTarget); const email = formData.get("email") as string; const password = formData.get("password") as string; await handleSignIn(email, password, "credentials"); }; /** * Handles quick guest login securely via Server Action. * * @async * @returns {Promise} */ const handleGuestLogin = async () => { setError(null); setLoadingType("guest"); try { const result = await getGuestCredentials(); if ("error" in result && result.error) { setError(result.error); setLoadingType(null); return; } if (!result.email || !result.password) { setError("Guest login is not configured properly."); setLoadingType(null); return; } await handleSignIn(result.email, result.password, "guest"); } catch (err) { setError("Failed to initialize guest login."); setLoadingType(null); } }; return (
{/* Login Card Container */}
{/* Header Section */}

Flowstate Login

Sign in to continue.

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