2026-02-11 19:29:47 +01:00
|
|
|
import { useState } from "react";
|
2026-02-11 18:36:05 +01:00
|
|
|
import { Navigate, useNavigate } from "react-router";
|
2026-02-11 19:29:47 +01:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import { z } from "zod";
|
2026-02-11 21:33:34 +01:00
|
|
|
import { signIn, useSession } from "@/lib/auth-client";
|
|
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "@/components/ui/card";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
2026-02-24 21:15:51 +01:00
|
|
|
import ErrorAlert from "@/components/ErrorAlert";
|
|
|
|
|
import ErrorMessage from "@/components/ErrorMessage";
|
|
|
|
|
import { Loader2 } from "lucide-react";
|
2026-02-11 18:36:05 +01:00
|
|
|
|
2026-02-11 19:29:47 +01:00
|
|
|
const loginSchema = z.object({
|
|
|
|
|
email: z.email("Please enter a valid email"),
|
|
|
|
|
password: z.string().min(1, "Password is required"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
type LoginFormData = z.infer<typeof loginSchema>;
|
|
|
|
|
|
2026-02-11 18:36:05 +01:00
|
|
|
export default function LoginPage() {
|
|
|
|
|
const { data: session, isPending } = useSession();
|
|
|
|
|
const navigate = useNavigate();
|
2026-02-11 19:29:47 +01:00
|
|
|
const [serverError, setServerError] = useState("");
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
|
} = useForm<LoginFormData>({
|
|
|
|
|
resolver: zodResolver(loginSchema),
|
|
|
|
|
});
|
2026-02-11 18:36:05 +01:00
|
|
|
|
|
|
|
|
if (isPending) {
|
2026-02-11 20:21:34 +01:00
|
|
|
return (
|
2026-02-11 21:33:34 +01:00
|
|
|
<div className="flex items-center justify-center h-screen text-muted-foreground">
|
|
|
|
|
<Loader2 className="animate-spin mr-2 h-5 w-5" />
|
2026-02-11 20:21:34 +01:00
|
|
|
Loading...
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-02-11 18:36:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
|
return <Navigate to="/" replace />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 19:29:47 +01:00
|
|
|
const onSubmit = async (data: LoginFormData) => {
|
|
|
|
|
setServerError("");
|
2026-02-11 18:36:05 +01:00
|
|
|
|
2026-02-11 19:29:47 +01:00
|
|
|
const { error } = await signIn.email(data);
|
2026-02-11 18:36:05 +01:00
|
|
|
|
|
|
|
|
if (error) {
|
2026-02-11 19:29:47 +01:00
|
|
|
setServerError(error.message ?? "Login failed");
|
2026-02-11 18:36:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
navigate("/", { replace: true });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-02-11 21:33:34 +01:00
|
|
|
<div className="flex items-center justify-center min-h-screen bg-muted/50">
|
|
|
|
|
<Card className="w-full max-w-[400px]">
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="text-2xl">Helpdesk</CardTitle>
|
|
|
|
|
<CardDescription>Sign in to your account</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} noValidate>
|
|
|
|
|
{serverError && (
|
2026-02-24 21:15:51 +01:00
|
|
|
<ErrorAlert message={serverError} className="mb-4" />
|
2026-02-11 21:33:34 +01:00
|
|
|
)}
|
|
|
|
|
<div className="grid gap-4">
|
|
|
|
|
<div className="grid gap-2">
|
|
|
|
|
<Label htmlFor="email">Email</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="admin@example.com"
|
|
|
|
|
{...register("email")}
|
|
|
|
|
/>
|
|
|
|
|
{errors.email && (
|
2026-02-24 21:15:51 +01:00
|
|
|
<ErrorMessage message={errors.email.message} />
|
2026-02-11 21:33:34 +01:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="grid gap-2">
|
|
|
|
|
<Label htmlFor="password">Password</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="Enter your password"
|
|
|
|
|
{...register("password")}
|
|
|
|
|
/>
|
|
|
|
|
{errors.password && (
|
2026-02-24 21:15:51 +01:00
|
|
|
<ErrorMessage message={errors.password.message} />
|
2026-02-11 21:33:34 +01:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<Button type="submit" className="w-full" disabled={isSubmitting}>
|
|
|
|
|
{isSubmitting && (
|
|
|
|
|
<Loader2 className="animate-spin mr-2 h-4 w-4" />
|
|
|
|
|
)}
|
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign in"}
|
|
|
|
|
</Button>
|
2026-02-11 20:21:34 +01:00
|
|
|
</div>
|
2026-02-11 21:33:34 +01:00
|
|
|
</form>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
2026-02-11 18:36:05 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|