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 18:36:05 +01:00
|
|
|
import { signIn, useSession } from "../lib/auth-client";
|
|
|
|
|
|
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) {
|
|
|
|
|
return <div className="loading">Loading...</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<div className="login-container">
|
|
|
|
|
<div className="login-card">
|
|
|
|
|
<h1>Helpdesk</h1>
|
|
|
|
|
<p className="login-subtitle">Sign in to your account</p>
|
2026-02-11 19:29:47 +01:00
|
|
|
<form onSubmit={handleSubmit(onSubmit)} noValidate>
|
|
|
|
|
{serverError && <div className="error-message">{serverError}</div>}
|
2026-02-11 18:36:05 +01:00
|
|
|
<label htmlFor="email">Email</label>
|
|
|
|
|
<input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
2026-02-11 19:29:47 +01:00
|
|
|
className={errors.email ? "input-error" : ""}
|
2026-02-11 18:36:05 +01:00
|
|
|
placeholder="admin@example.com"
|
2026-02-11 19:29:47 +01:00
|
|
|
{...register("email")}
|
2026-02-11 18:36:05 +01:00
|
|
|
/>
|
2026-02-11 19:29:47 +01:00
|
|
|
{errors.email && (
|
|
|
|
|
<div className="field-error">{errors.email.message}</div>
|
|
|
|
|
)}
|
2026-02-11 18:36:05 +01:00
|
|
|
<label htmlFor="password">Password</label>
|
|
|
|
|
<input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
2026-02-11 19:29:47 +01:00
|
|
|
className={errors.password ? "input-error" : ""}
|
2026-02-11 18:36:05 +01:00
|
|
|
placeholder="Enter your password"
|
2026-02-11 19:29:47 +01:00
|
|
|
{...register("password")}
|
2026-02-11 18:36:05 +01:00
|
|
|
/>
|
2026-02-11 19:29:47 +01:00
|
|
|
{errors.password && (
|
|
|
|
|
<div className="field-error">{errors.password.message}</div>
|
|
|
|
|
)}
|
|
|
|
|
<button
|
|
|
|
|
className="btn btn-primary"
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
>
|
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign in"}
|
2026-02-11 18:36:05 +01:00
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|