import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import apiClient from '../../api/client'; import { ArrowRight } from 'lucide-react'; interface AuthPageProps { onLoginSuccess: () => void; } export function AuthPage({ onLoginSuccess }: AuthPageProps) { const { t } = useTranslation(); const [isLogin, setIsLogin] = useState(true); const [userName, setUserName] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setLoading(true); try { if (isLogin) { const response = await apiClient.post('/api/v1/auth/login', { user_name: userName, password: password, }); if (response.data.token) { localStorage.setItem('token', response.data.token); onLoginSuccess(); } } else { const response = await apiClient.post('/api/v1/auth/register', { user_name: userName, password: password, }); if (response.data.message === 'success') { setIsLogin(true); setError(t('auth.registerSuccess')); } } } catch (err: any) { console.error(err); setError(err.response?.data?.detail || err.response?.data?.message || t('auth.authFailed')); } finally { setLoading(false); } }; return (
{t('app.tagline')}
{isLogin ? t('auth.enterCredentials') : t('auth.signUpToStart')}
{error && ({isLogin ? t('auth.noAccount') : t('auth.hasAccount')}{' '}