"use client" import Altcha from "@/components/core/altcha"; import { Nav } from "@/components/core/nav"; import { TbLogin } from "react-icons/tb"; import { useState, useEffect, Suspense } from "react"; import { useForm, SubmitHandler } from "react-hook-form"; import { useRouter, useSearchParams } from "next/navigation"; import { authClient } from "@/util/auth-client"; interface LoginForm { email: string; password: string; } function LoginForm() { const router = useRouter(); const searchParams = useSearchParams(); const [altchaState, setAltchaState] = useState<{ status: "success" | "error" | "expired" | "waiting", token: string }>({ status: "waiting", token: "" }); const [isLoading, setIsLoading] = useState(false); const [apiError, setApiError] = useState(""); const [successMessage, setSuccessMessage] = useState(""); const { register, handleSubmit, formState: { errors }, } = useForm(); useEffect(() => { const message = searchParams.get('message'); if (message) { setSuccessMessage(message); } }, [searchParams]); const onSubmit: SubmitHandler = async (data) => { if (altchaState.status !== "success") { setApiError("Please complete the captcha"); return; } setIsLoading(true); setApiError(""); try { const response = await fetch("/api/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ ...data, token: altchaState.token, }), }); const result = await response.json(); if (!response.ok) { setApiError(result.error || "Failed to sign in"); return; } try { await authClient.signIn.email({ email: data.email, password: data.password, }); const redirectTo = searchParams.get('redirect') || "/"; router.push(redirectTo); } catch (signInError) { console.error("Auth client sign in failed:", signInError); const redirectTo = searchParams.get('redirect') || "/"; router.push(redirectTo); } } catch (error) { console.error("Login error:", error); setApiError("An unexpected error occurred. Please try again."); } finally { setIsLoading(false); } }; const handleAltchaStateChange = (e: Event | CustomEvent) => { if ('detail' in e && e.detail?.payload) { setAltchaState({ status: "success", token: e.detail.payload }); } else { setAltchaState({ status: "error", token: "" }); } }; return (

login

{successMessage && (
{successMessage}
)}

email

{errors.email &&

{errors.email.message}

}

password

{errors.password &&

{errors.password.message}

}

captcha

{apiError && (
{apiError}
)}

Don't have an account?{" "} Sign up here

); } export default function Login() { return (
); }