"use client" import { useState, useEffect, Suspense } from "react" import { signIn } from "next-auth/react" import { useRouter, useSearchParams } from "next/navigation" import Link from "next/link" function LoginForm() { const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) const router = useRouter() const searchParams = useSearchParams() const isDemo = searchParams.get("demo") === "1" const doLogin = async (e?: React.FormEvent, demoEmail?: string, demoPass?: string) => { if (e) e.preventDefault() setError("") setLoading(true) const result = await signIn("credentials", { email: demoEmail || email, password: demoPass || password, redirect: false, }) if (result?.error) { setError("Invalid email or password") setLoading(false) } else { // Role-aware redirect: community leaders go to their scoped dashboard try { const session = await fetch("/api/auth/session").then(r => r.json()) const role = session?.user?.role if (role === "community_leader" || role === "volunteer") { router.push("/dashboard/community") } else { router.push("/dashboard") } } catch { router.push("/dashboard") } } } // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(() => { if (isDemo) doLogin(undefined, "demo@pnpl.app", "demo1234") }, []) return (
Loading demo...
Sign in to your charity dashboard
Don't have an account?{" "} Get Started Free
> )}