import { auth } from '../api.js'; import { icons } from '../icons.js'; export function renderLogin(container, isSetup, onLogin) { container.innerHTML = `
${isSetup ? '
Utilizza una password forte con almeno 12 caratteri
' : ''}
${isSetup ? `
` : ''}
`; const form = document.getElementById('login-form'); const errorEl = document.getElementById('login-error'); form.onsubmit = async (e) => { e.preventDefault(); errorEl.classList.add('hidden'); const btn = document.getElementById('login-submit'); btn.disabled = true; btn.textContent = '...'; const username = document.getElementById('login-username').value; const password = document.getElementById('login-password').value; try { if (isSetup) { const confirm = document.getElementById('login-confirm').value; if (password !== confirm) throw new Error('Le password non coincidono'); if (password.length < 12) throw new Error('La password deve avere almeno 12 caratteri'); const data = await auth.setup(username, password); onLogin(data.user); } else { const data = await auth.login(username, password); onLogin(data.user); } } catch (err) { errorEl.textContent = err.message || 'Errore di autenticazione'; errorEl.classList.remove('hidden'); btn.disabled = false; btn.textContent = isSetup ? 'Crea Account' : 'Accedi'; } }; }