Initial commit
This commit is contained in:
71
components/auth/login-form.tsx
Normal file
71
components/auth/login-form.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
"use client";
|
||||
|
||||
import { FormEvent, useState } from "react";
|
||||
import { signIn } from "next-auth/react";
|
||||
import type { Route } from "next";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
export function LoginForm() {
|
||||
const router = useRouter();
|
||||
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [pending, setPending] = useState(false);
|
||||
|
||||
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setError(null);
|
||||
setPending(true);
|
||||
|
||||
const result = await signIn("credentials", {
|
||||
email,
|
||||
password,
|
||||
redirect: false
|
||||
});
|
||||
|
||||
setPending(false);
|
||||
|
||||
if (!result || result.error) {
|
||||
setError("Invalid email or password");
|
||||
return;
|
||||
}
|
||||
|
||||
router.push("/dashboard" as Route);
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="space-y-4" onSubmit={onSubmit}>
|
||||
<label className="block space-y-2 text-sm">
|
||||
<span className="text-muted">Email</span>
|
||||
<Input
|
||||
required
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
value={email}
|
||||
onChange={(event) => setEmail(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label className="block space-y-2 text-sm">
|
||||
<span className="text-muted">Password</span>
|
||||
<Input
|
||||
required
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
value={password}
|
||||
onChange={(event) => setPassword(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
|
||||
{error ? <p className="text-sm text-red-300">{error}</p> : null}
|
||||
|
||||
<Button type="submit" variant="primary" className="w-full" disabled={pending}>
|
||||
{pending ? "Signing in..." : "Sign in"}
|
||||
</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user