Files
Medasset_ai/frontend/src/pages/LoginPage.tsx
T

190 lines
7.2 KiB
TypeScript

import { useState } from "react";
import { Navigate, useNavigate } from "react-router-dom";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { AlertCircle, LogIn, ShieldCheck } from "lucide-react";
import { loginApi } from "@/api/auth";
import { ApiError } from "@/api/client";
import { useAuthStore } from "@/hooks/useAuth";
import { Button } from "@/components/ui/button";
const loginSchema = z.object({
email: z.string().email("Enter a valid email address"),
password: z.string().min(1, "Password is required"),
});
type LoginFormValues = z.infer<typeof loginSchema>;
const DEMO_ACCOUNTS = [
{ label: "System Admin", email: "admin@xyz.demo", hint: "Full system control" },
{ label: "Biomedical", email: "bio@xyz.demo", hint: "Assets, PM, repairs" },
{ label: "Department User (ICU)", email: "dept@xyz.demo", hint: "ICU complaints & assets" },
{ label: "Management", email: "mgmt@xyz.demo", hint: "Reports & oversight" },
{ label: "Coordinator", email: "coord@xyz.demo", hint: "Contracts, demo, calibration" },
];
export function LoginPage() {
const navigate = useNavigate();
const { user, token, setAuth } = useAuthStore();
const [serverError, setServerError] = useState<string | null>(null);
const [quickLoadingEmail, setQuickLoadingEmail] = useState<string | null>(null);
const {
register,
handleSubmit,
setValue,
formState: { errors, isSubmitting },
} = useForm<LoginFormValues>({
resolver: zodResolver(loginSchema),
defaultValues: { email: "", password: "" },
});
if (token && user) {
return <Navigate to="/" replace />;
}
async function doLogin(email: string, password: string) {
setServerError(null);
try {
const res = await loginApi(email, password);
setAuth(res.user, res.access_token);
navigate("/", { replace: true });
} catch (err) {
if (err instanceof ApiError) {
setServerError(err.detail);
} else {
setServerError("Unable to reach server. Please ensure the backend is running.");
}
}
}
async function onSubmit(values: LoginFormValues) {
await doLogin(values.email, values.password);
}
async function handleQuickLogin(email: string) {
setValue("email", email);
setValue("password", "Demo@123");
setQuickLoadingEmail(email);
try {
await doLogin(email, "Demo@123");
} finally {
setQuickLoadingEmail(null);
}
}
return (
<div className="min-h-screen bg-gradient-to-br from-[#E8EFF7] via-[#F8FAFC] to-[#E8EFF7]/60 flex items-center justify-center p-4">
<div className="w-full max-w-md space-y-5">
<div className="bg-white rounded-xl shadow-md border border-[#E8EFF7] p-6 sm:p-8">
{/* Hospital Branding */}
<div className="text-center mb-6">
<img
src="/logo.svg"
alt="XYZ Hospital"
onError={(e) => {
(e.target as HTMLImageElement).src = "/logo.jpg";
}}
className="h-20 mx-auto object-contain mb-3"
/>
<h1 className="text-xl font-bold text-[#102C52]">
XYZ Hospital
</h1>
<div className="inline-flex items-center gap-1.5 mt-1 px-2.5 py-0.5 rounded-full bg-[#E8EFF7] text-xs font-semibold text-[#008E95]">
<span className="w-1.5 h-1.5 rounded-full bg-[#008E95]"></span>
<span>MediAsset AI</span>
</div>
<p className="text-xs text-gray-500 mt-1">
Asset Management &amp; Maintenance System
</p>
</div>
{serverError && (
<div
role="alert"
className="mb-4 flex items-start gap-2 rounded-md bg-red-50 border border-red-200 px-3 py-2.5 text-sm text-red-700"
>
<AlertCircle className="h-4 w-4 shrink-0 mt-0.5" />
<span>{serverError}</span>
</div>
)}
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4" noValidate>
<div>
<label htmlFor="email" className="block text-sm font-medium text-[#102C52] mb-1">
Email Address
</label>
<input
id="email"
type="email"
autoComplete="email"
placeholder="you@xyz.demo"
{...register("email")}
className="w-full px-3 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-[#2E5C9A]"
/>
{errors.email && (
<p className="text-xs text-red-600 mt-1">{errors.email.message}</p>
)}
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-[#102C52] mb-1">
Password
</label>
<input
id="password"
type="password"
autoComplete="current-password"
placeholder="Enter your password"
{...register("password")}
className="w-full px-3 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-[#2E5C9A]"
/>
{errors.password && (
<p className="text-xs text-red-600 mt-1">{errors.password.message}</p>
)}
</div>
<Button type="submit" className="w-full bg-[#2E5C9A] hover:bg-[#102C52] text-white transition-colors" loading={isSubmitting}>
<LogIn className="h-4 w-4 mr-2" />
Sign In
</Button>
</form>
</div>
{/* Quick Demo Login Panel */}
<div className="bg-white rounded-xl shadow-sm border border-[#E8EFF7] p-4">
<div className="flex items-center gap-1.5 text-xs font-semibold text-[#102C52] uppercase tracking-wide mb-2.5">
<ShieldCheck className="h-4 w-4 text-[#008E95]" />
Quick Demo Login (Password: Demo@123)
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
{DEMO_ACCOUNTS.map((acct) => (
<button
key={acct.email}
type="button"
disabled={isSubmitting || quickLoadingEmail !== null}
onClick={() => handleQuickLogin(acct.email)}
className="text-left px-3 py-2 rounded-md border border-gray-200 hover:border-[#2E5C9A] hover:bg-[#E8EFF7]/50 transition-colors disabled:opacity-50"
>
<div className="text-xs font-semibold text-gray-800 flex items-center justify-between">
<span>{acct.label}</span>
{quickLoadingEmail === acct.email && (
<span className="text-[10px] text-[#2E5C9A] font-medium">Signing in…</span>
)}
</div>
<div className="text-[11px] text-gray-500 truncate">{acct.email}</div>
</button>
))}
</div>
</div>
{/* Brand & Clearance Disclaimer */}
<div className="text-center text-[11px] text-gray-400">
The name, tagline, logo concept, and palette are proposed brand assets and remain subject to clearance.
</div>
</div>
</div>
);
}