import { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Eye, EyeOff, Loader2 } from "lucide-react"; import { useAuth } from "@/contexts/AuthContext"; import { useToast } from "@/hooks/use-toast"; import { ApiError } from "@/lib/api-client"; import type { UserRole } from "@/types/auth"; /** Keep in sync with `ProtectedRoute` post-login targets */ function getRoleDashboard(role: string): string { switch (role as UserRole) { case "student": return "/student/dashboard"; case "teacher": return "/teacher/dashboard"; case "admin": case "developer": return "/admin/dashboard"; case "corporate": case "mastercorporate": case "agent": return "/admin/platform"; default: return "/admin/dashboard"; } } function loginErrorMessage(err: unknown): string { if (err instanceof ApiError && err.data && typeof err.data === "object" && err.data !== null && "error" in err.data) { return String((err.data as { error: string }).error); } if (err instanceof Error) { return err.message; } return "Login failed"; } export default function Login() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const navigate = useNavigate(); const { login } = useAuth(); const { toast } = useToast(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!email || !password) { toast({ title: "Error", description: "Please enter email and password", variant: "destructive" }); return; } setLoading(true); try { const user = await login(email, password); navigate(getRoleDashboard(user.user_type)); } catch (err: unknown) { toast({ title: "Login Failed", description: loginErrorMessage(err), variant: "destructive" }); } finally { setLoading(false); } }; return (
Don't have an account?{" "} Sign up