RJBULK-APP/components/forms/auth-form.tsx

57 lines
2 KiB
TypeScript
Raw Normal View History

"use client";
import { useActionState, useEffect } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { loginAction, type LoginResult } from "@/actions/auth";
import { toast } from "sonner";
const initial: LoginResult = { ok: false, error: "" };
export function LoginForm() {
const router = useRouter();
const [state, formAction, pending] = useActionState(loginAction, initial);
useEffect(() => {
if (!state.ok) {
toast.error(state.error);
}
}, [state]);
return (
<Card>
<CardHeader>
<CardTitle className="text-2xl">Sign in</CardTitle>
<CardDescription>Enter your credentials to access RJ Bulk.</CardDescription>
</CardHeader>
<form action={formAction}>
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="identity">Username or email</Label>
<Input id="identity" name="identity" type="text" autoComplete="username" required autoFocus />
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input id="password" name="password" type="password" autoComplete="current-password" required />
</div>
</CardContent>
<CardFooter className="flex flex-col gap-3">
<Button type="submit" className="w-full" disabled={pending}>
{pending ? "Signing in…" : "Sign in"}
</Button>
<p className="text-sm text-muted-foreground">
New here?{" "}
<Link href="/register" className="text-primary underline-offset-4 hover:underline">
Create an account
</Link>
</p>
</CardFooter>
</form>
</Card>
);
}