"use client";
import { useState, useEffect } from "react";
import { signUp, useSession } from "@/lib/auth-client";
import { useRouter } from "next/navigation";
import Link from "next/link";
export default function SignUpPage() {
const router = useRouter();
const { data: session, isPending } = useSession();
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
// Redirect already-authenticated users to the dashboard.
useEffect(() => {
if (!isPending && session) {
router.push("/dashboard");
}
}, [isPending, session, router]);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError("");
setLoading(true);
await signUp.email(
{ name, email, password },
{
onSuccess: () => {
router.push("/dashboard");
},
onError: (ctx) => {
setError(ctx.error.message || "Sign up failed");
},
},
);
setLoading(false);
}
return (
<div className="container" style={{ marginTop: "4rem" }}>
<div className="card">
<h2>Create Account</h2>
<p className="muted">Sign up with your email and password.</p>
{error && <div className="error">{error}</div>}
<form onSubmit={handleSubmit}>
<label htmlFor="name">Name</label>
<input
id="name"
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Your name"
required
/>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="you@example.com"
required
/>
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="At least 8 characters"
required
minLength={8}
/>
<button type="submit" disabled={loading}>
{loading ? "Creating account..." : "Sign Up"}
</button>
</form>
<div className="form-footer">
Already have an account? <Link href="/sign-in">Sign in</Link>
</div>
</div>
</div>
);
}