initial commit
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
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@poona.demo", hint: "Full system control" },
|
||||
{ label: "Biomedical", email: "bio@poona.demo", hint: "Assets, PM, repairs" },
|
||||
{ label: "Department User (ICU)", email: "dept@poona.demo", hint: "ICU complaints & assets" },
|
||||
{ label: "Management", email: "mgmt@poona.demo", hint: "Reports & oversight" },
|
||||
{ label: "Coordinator", email: "coord@poona.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-blue-50 via-gray-50 to-indigo-50 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-gray-200 p-6 sm:p-8">
|
||||
{/* Hospital Branding */}
|
||||
<div className="text-center mb-6">
|
||||
<img
|
||||
src="/logo.jpg"
|
||||
alt="Poona Hospital and Research Centre"
|
||||
className="h-20 mx-auto object-contain mb-3"
|
||||
/>
|
||||
<h1 className="text-xl font-bold text-[#1e3a8a]">
|
||||
Poona Hospital & Research Centre
|
||||
</h1>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Asset Management & 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-gray-700 mb-1">
|
||||
Email Address
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
placeholder="you@poona.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-blue-500"
|
||||
/>
|
||||
{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-gray-700 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-blue-500"
|
||||
/>
|
||||
{errors.password && (
|
||||
<p className="text-xs text-red-600 mt-1">{errors.password.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button type="submit" className="w-full" 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-gray-200 p-4">
|
||||
<div className="flex items-center gap-1.5 text-xs font-semibold text-gray-600 uppercase tracking-wide mb-2.5">
|
||||
<ShieldCheck className="h-4 w-4 text-[#1e3a8a]" />
|
||||
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-[#1e3a8a] hover:bg-blue-50/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-blue-600">Signing in…</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-[11px] text-gray-500 truncate">{acct.email}</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user