import React, { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { useAuthStore } from '../store/authStore' import { useSettingsStore } from '../store/settingsStore' import { useTranslation } from '../i18n' import { authApi } from '../api/client' import { Plane, Eye, EyeOff, Mail, Lock, MapPin, Calendar, Package, User, Globe } from 'lucide-react' export default function LoginPage() { const { t, language } = useTranslation() const [mode, setMode] = useState('login') // 'login' | 'register' const [username, setUsername] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [showPassword, setShowPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const [appConfig, setAppConfig] = useState(null) const { login, register } = useAuthStore() const { setLanguageLocal } = useSettingsStore() const navigate = useNavigate() useEffect(() => { authApi.getAppConfig?.().catch(() => null).then(config => { if (config) { setAppConfig(config) if (!config.has_users) setMode('register') } }) }, []) const handleSubmit = async (e) => { e.preventDefault() setError('') setIsLoading(true) try { if (mode === 'register') { if (!username.trim()) { setError('Username is required'); setIsLoading(false); return } if (password.length < 6) { setError('Password must be at least 6 characters'); setIsLoading(false); return } await register(username, email, password) } else { await login(email, password) } navigate('/dashboard') } catch (err) { setError(err.message || t('login.error')) } finally { setIsLoading(false) } } const showRegisterOption = appConfig?.allow_registration || !appConfig?.has_users const inputBase = { width: '100%', padding: '11px 12px 11px 40px', border: '1px solid #e5e7eb', borderRadius: 12, fontSize: 14, fontFamily: 'inherit', outline: 'none', color: '#111827', background: 'white', boxSizing: 'border-box', transition: 'border-color 0.15s', } return (
{t('login.description')}
{mode === 'register' ? (!appConfig?.has_users ? t('login.createAdminHint') : t('login.createAccountHint')) : t('login.subtitle')}
{/* Toggle login/register */} {showRegisterOption && appConfig?.has_users && ({mode === 'login' ? t('login.noAccount') + ' ' : t('login.hasAccount') + ' '}
)}