import { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { useAuth } from '../contexts/AuthContext' import { useLanguage } from '../i18n/LanguageContext' import { Megaphone, Lock, Mail, AlertCircle, User, CheckCircle } from 'lucide-react' import { api } from '../utils/api' export default function Login() { const navigate = useNavigate() const { login } = useAuth() const { t } = useLanguage() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [needsSetup, setNeedsSetup] = useState(null) const [setupName, setSetupName] = useState('') const [setupEmail, setSetupEmail] = useState('') const [setupPassword, setSetupPassword] = useState('') const [setupDone, setSetupDone] = useState(false) useEffect(() => { api.get('/setup/status').then(data => setNeedsSetup(data.needsSetup)).catch(() => setNeedsSetup(false)) }, []) const handleSubmit = async (e) => { e.preventDefault() setError('') setLoading(true) try { await login(email, password) navigate('/') } catch (err) { setError(err.message || 'Invalid email or password') } finally { setLoading(false) } } const handleSetup = async (e) => { e.preventDefault() setError('') setLoading(true) try { await api.post('/setup', { name: setupName, email: setupEmail, password: setupPassword }) setSetupDone(true) setNeedsSetup(false) setEmail(setupEmail) } catch (err) { setError(err.message || 'Setup failed') } finally { setLoading(false) } } if (needsSetup === null) { return (
{needsSetup ? 'Create your superadmin account to get started' : t('login.subtitle')}
Account created. You can now log in.
{t('login.forgotPassword')}