82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* SignUpForm — paired with LoginForm. Same callback contract:
|
|
* `onSuccess` fires once the new account is logged in; callers decide
|
|
* what to do next.
|
|
*/
|
|
|
|
import { useState } from "react";
|
|
import { useAuth } from "@/components/auth/auth-provider";
|
|
import { AuthError } from "@/lib/auth";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
export function SignUpForm({ onSuccess }: { onSuccess?: () => void }) {
|
|
const { signUp } = useAuth();
|
|
const [name, setName] = useState("");
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
async function onSubmit(e: React.SyntheticEvent) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setSubmitting(true);
|
|
try {
|
|
await signUp(email, password, name);
|
|
onSuccess?.();
|
|
} catch (err) {
|
|
setError(err instanceof AuthError ? err.message : "Sign-up failed.");
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={onSubmit} className="space-y-5">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="signup-name">Name</Label>
|
|
<Input
|
|
id="signup-name"
|
|
type="text"
|
|
autoComplete="name"
|
|
required
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="signup-email">Email</Label>
|
|
<Input
|
|
id="signup-email"
|
|
type="email"
|
|
autoComplete="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="signup-password">Password</Label>
|
|
<Input
|
|
id="signup-password"
|
|
type="password"
|
|
autoComplete="new-password"
|
|
minLength={8}
|
|
required
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
/>
|
|
<p className="text-xs text-muted-foreground">At least 8 characters.</p>
|
|
</div>
|
|
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
|
<Button type="submit" disabled={submitting} className="w-full h-11 text-base">
|
|
{submitting ? "Creating account…" : "Sign up"}
|
|
</Button>
|
|
</form>
|
|
);
|
|
}
|