96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
"use client";
|
|
|
|
import { useState, type FormEvent } from "react";
|
|
import { signIn } from "next-auth/react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
// Custom Credentials sign-in page for Auth.js v5 (src/auth.ts's
|
|
// pages.signIn points here). Internal IT-staff login only — no
|
|
// self-registration, so there's no "sign up" link. Deliberately shows a
|
|
// generic error on failure (doesn't say whether the email or the password
|
|
// was wrong) to avoid leaking which accounts exist.
|
|
export default function LoginPage() {
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
async function handleSubmit(event: FormEvent<HTMLFormElement>): Promise<void> {
|
|
event.preventDefault();
|
|
setError(null);
|
|
setSubmitting(true);
|
|
|
|
try {
|
|
const result = await signIn("credentials", {
|
|
email,
|
|
password,
|
|
redirect: false,
|
|
});
|
|
|
|
if (!result || result.error) {
|
|
setError("Invalid email or password");
|
|
return;
|
|
}
|
|
|
|
window.location.href = "/";
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center p-4">
|
|
<form
|
|
onSubmit={handleSubmit}
|
|
className="w-full max-w-sm space-y-4 rounded-lg border border-border p-6"
|
|
>
|
|
<div className="space-y-1">
|
|
<h1 className="text-lg font-semibold">Sign in</h1>
|
|
<p className="text-sm text-muted-foreground">IT staff login</p>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<label htmlFor="email" className="text-sm font-medium">
|
|
Email
|
|
</label>
|
|
<Input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
required
|
|
value={email}
|
|
onChange={(event) => setEmail(event.target.value)}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<label htmlFor="password" className="text-sm font-medium">
|
|
Password
|
|
</label>
|
|
<Input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
value={password}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
/>
|
|
</div>
|
|
|
|
{error ? (
|
|
<p role="alert" className="text-sm text-destructive">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
|
|
<Button type="submit" className="w-full" disabled={submitting}>
|
|
{submitting ? "Signing in..." : "Sign in"}
|
|
</Button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|