'use client' import { useState } from 'react' import { useStore } from '@/lib/store' interface LoginFormProps { onSuccess?: () => void } export function LoginForm({ onSuccess }: LoginFormProps) { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const { setUser } = useStore() const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError('') try { const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }) const data = await response.json() if (!response.ok) { setError(data.error || 'Eroare la autentificare') return } // Store user and token setUser(data.user) localStorage.setItem('authToken', data.token) if (onSuccess) { onSuccess() } } catch (error) { setError('Eroare de conexiune') } finally { setLoading(false) } } return (
setEmail(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" required />
setPassword(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" required />
{error && (
{error}
)}
) }