"use client" import { useState } from "react" import { signIn, signOut, useSession } from "next-auth/react" export default function AuthPanel() { const { data: session } = useSession() const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [name, setName] = useState("") const [loading, setLoading] = useState(false) const [msg, setMsg] = useState("") const doSignup = async () => { setLoading(true) setMsg("") try { const res = await fetch("/api/signup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password, name }), }) if (!res.ok) { const j = await res.json().catch(() => ({})) throw new Error(j?.error || `HTTP ${res.status}`) } setMsg("Signed up. Now log in.") } catch (e: any) { setMsg(e?.message || "Failed") } finally { setLoading(false) } } const doLogin = async () => { setLoading(true) setMsg("") const res = await signIn("credentials", { email, password, redirect: false }) if (res?.error) setMsg(res.error) setLoading(false) } if (session?.user) { return (
Logged in as {session.user.email}
) } return (
Credentials auth (SQLite)
setName(e.target.value)} /> setEmail(e.target.value)} /> setPassword(e.target.value)} />
{msg &&
{msg}
}
) }