import React, { useState, useEffect } from 'react'; import { useAuth } from '../contexts/AuthContext'; import { Users, Lock, Eye, EyeOff, XCircle, Mail, ArrowRight, CheckCircle, X, Sparkles, Shield, KeyRound } from 'lucide-react'; export const LoginPage: React.FC = () => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [rememberMe, setRememberMe] = useState(false); const [error, setError] = useState(''); const [showError, setShowError] = useState(false); const [loading, setLoading] = useState(false); const { login } = useAuth(); // Forgot password modal state const [showForgotModal, setShowForgotModal] = useState(false); const [forgotEmail, setForgotEmail] = useState(''); const [forgotLoading, setForgotLoading] = useState(false); const [forgotSuccess, setForgotSuccess] = useState(false); const [forgotError, setForgotError] = useState(''); // Auto-hide error after 5 seconds useEffect(() => { if (error) { setShowError(true); const timer = setTimeout(() => { setShowError(false); setTimeout(() => setError(''), 300); }, 5000); return () => clearTimeout(timer); } }, [error]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setLoading(true); try { await login(username, password); } catch (err: unknown) { const error = err as Error; const errorMessage = error.message?.includes('401') || error.message?.includes('Unauthorized') || error.message?.includes('Invalid') ? 'Invalid username or password' : error.message || 'Login failed. Please check your credentials.'; setError(errorMessage); console.error('Login error:', err); } finally { setLoading(false); } }; const handleForgotPassword = async (e: React.FormEvent) => { e.preventDefault(); setForgotLoading(true); setForgotError(''); // Simulate API call (replace with actual API call) try { await new Promise(resolve => setTimeout(resolve, 1500)); // In a real app, you'd call: await api.requestPasswordReset(forgotEmail); setForgotSuccess(true); } catch { setForgotError('Failed to send reset email. Please try again.'); } finally { setForgotLoading(false); } }; const closeForgotModal = () => { setShowForgotModal(false); setForgotEmail(''); setForgotSuccess(false); setForgotError(''); }; return (
Sign in to your account to continue
Login Failed
{error}
Enter your email address and we'll send you instructions to reset your password.
We've sent password reset instructions to
{forgotEmail}