Organize/components/auth/signup-form.tsx

76 lines
2.5 KiB
TypeScript

"use client";
import { useActionState } from "react";
import Link from "next/link";
import { signup } from "@/lib/actions/auth";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { SignupMode } from "@/lib/generated/prisma/enums";
export function SignupForm({ mode }: { mode: SignupMode }) {
const [state, action, pending] = useActionState(signup, undefined);
if (state?.pending) {
return (
<Card>
<CardHeader>
<CardTitle className="text-2xl">Account created</CardTitle>
<CardDescription>
An administrator needs to approve your account before you can log in. Check back
soon.
</CardDescription>
</CardHeader>
</Card>
);
}
return (
<Card>
<CardHeader>
<CardTitle className="text-2xl">Create your account</CardTitle>
<CardDescription>
{mode === SignupMode.APPROVED
? "An administrator will need to approve your account before you can log in."
: "Start organizing in a minute."}
</CardDescription>
</CardHeader>
<CardContent>
<form action={action} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name">Name</Label>
<Input id="name" name="name" autoComplete="name" required />
</div>
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input id="email" name="email" type="email" autoComplete="email" required />
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
name="password"
type="password"
autoComplete="new-password"
minLength={8}
required
/>
</div>
{state?.error && <p className="text-sm text-destructive">{state.error}</p>}
<Button type="submit" className="w-full" disabled={pending}>
{pending ? "Creating account…" : "Sign up"}
</Button>
</form>
<p className="mt-4 text-center text-sm text-muted-foreground">
Already have an account?{" "}
<Link href="/login" className="text-primary underline-offset-4 hover:underline">
Log in
</Link>
</p>
</CardContent>
</Card>
);
}