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-03-03 20:24:08 +01:00
|
|
|
<div className="flex items-center justify-center min-h-screen bg-background">
|
|
|
|
|
<div className="w-full max-w-[400px] px-4 animate-in-page">
|
|
|
|
|
<div className="flex flex-col items-center mb-10">
|
|
|
|
|
<div className="h-12 w-12 rounded-2xl bg-primary flex items-center justify-center mb-5">
|
|
|
|
|
<span className="text-primary-foreground font-bold text-xl">H</span>
|
|
|
|
|
</div>
|
|
|
|
|
<h1 className="text-2xl font-semibold tracking-tight">
|
|
|
|
|
Welcome back
|
|
|
|
|
</h1>
|
|
|
|
|
<p className="text-muted-foreground text-sm mt-1.5">
|
|
|
|
|
Sign in to your helpdesk account
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Sign in</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
Enter your credentials to continue
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} noValidate>
|
|
|
|
|
{serverError && (
|
|
|
|
|
<ErrorAlert message={serverError} className="mb-4" />
|
|
|
|
|
)}
|
|
|
|
|
<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 && (
|
|
|
|
|
<ErrorMessage message={errors.email.message} />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="grid gap-2">
|
|
|
|
|
<Label htmlFor="password">Password</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="Enter your password"
|
|
|
|
|
{...register("password")}
|
|
|
|
|
/>
|
|
|
|
|
{errors.password && (
|
|
|
|
|
<ErrorMessage message={errors.password.message} />
|
|
|
|
|
)}
|
|
|
|
|
</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 21:33:34 +01:00
|
|
|
</div>
|
2026-03-03 20:24:08 +01:00
|
|
|
</form>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
2026-02-11 18:36:05 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|