import React, { useState } from 'react'; import apiClient from '../../api/client'; import { Activity } from 'lucide-react'; interface AuthPageProps { onLoginSuccess: () => void; } export function AuthPage({ onLoginSuccess }: AuthPageProps) { 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) { // Login 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 { // Register const response = await apiClient.post('/api/v1/auth/register', { user_name: userName, password: password, }); // After successful register, we can automatically log them in // or just show a message and switch to login. Let's switch to login. if (response.data.message === 'success') { setIsLogin(true); setError('Registration successful. Please log in.'); } } } catch (err: any) { console.error(err); setError(err.response?.data?.detail || err.response?.data?.message || 'Authentication failed'); } finally { setLoading(false); } }; return (

{isLogin ? 'Welcome Back' : 'Create Account'}

{isLogin ? 'Enter your credentials to access your account' : 'Sign up to start using the platform'}

{error && (
{error}
)}
setUserName(e.target.value)} className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow" required />
setPassword(e.target.value)} className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow" required />
{isLogin ? "Don't have an account? " : "Already have an account? "}
); }