import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useMutation, useQuery } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { useAuth } from '../contexts/AuthContext'; import { useToast } from '../contexts/ToastContext'; import { useTheme } from '../contexts/ThemeContext'; import { X, Mail } from 'lucide-react'; import { api } from '../api/client'; import { Card, CardHeader, CardContent } from '../components/Card'; import { Button } from '../components/Button'; export function LoginPage() { const navigate = useNavigate(); const { t } = useTranslation(); const { login } = useAuth(); const { showToast } = useToast(); const { mode } = useTheme(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [showForgotPassword, setShowForgotPassword] = useState(false); const [forgotEmail, setForgotEmail] = useState(''); // Check if advanced auth is enabled const { data: advancedAuthStatus } = useQuery({ queryKey: ['advancedAuthStatus'], queryFn: () => api.getAdvancedAuthStatus(), }); const loginMutation = useMutation({ mutationFn: () => login(username, password), onSuccess: () => { showToast(t('login.loginSuccess')); navigate('/'); }, onError: (error: Error) => { showToast(error.message || t('login.loginFailed'), 'error'); }, }); const forgotPasswordMutation = useMutation({ mutationFn: (email: string) => api.forgotPassword({ email }), onSuccess: (data) => { showToast(data.message, 'success'); setShowForgotPassword(false); setForgotEmail(''); }, onError: (error: Error) => { showToast(error.message, 'error'); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!username || !password) { showToast(t('login.enterCredentials'), 'error'); return; } loginMutation.mutate(); }; const handleForgotPassword = (e: React.FormEvent) => { e.preventDefault(); if (!forgotEmail) { showToast('Please enter your email address', 'error'); return; } forgotPasswordMutation.mutate(forgotEmail); }; return (
{t('login.subtitle')}
{t('login.forgotPasswordMessage')}
{t('login.howToReset')}