import { handleApiError, updateAuthUI, setupLogout } from './utils.js'; document.getElementById('loginForm')?.addEventListener('submit', async (e) => { e.preventDefault(); try { const response = await fetch('/login', { method: 'POST', credentials: 'include', // dołącz ciasteczka headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: document.getElementById('loginEmail').value, haslo: document.getElementById('loginPassword').value }) }); if (response.ok) { window.location.href = '/'; } else { alert('Błąd logowania!'); } } catch (error) { console.error('Błąd:', error); } }); // Rejestracja document.getElementById('registerForm')?.addEventListener('submit', async (e) => { e.preventDefault(); const name = document.getElementById('registerName').value; const email = document.getElementById('registerEmail').value; const password = document.getElementById('registerPassword').value; const confirmPassword = document.getElementById('registerConfirmPassword').value; if (password !== confirmPassword) { alert('Hasła nie są identyczne'); return; } try { const response = await fetch('/rejestracja', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ imie: name, email, haslo: password, confirmPassword }), }); if (response.ok) { alert('Rejestracja udana! Możesz się teraz zalogować.'); window.location.href = '/login.html'; } else { throw new Error('Rejestracja nieudana'); } } catch (error) { handleApiError(error); } }); async function checkAuthStatus() { try { const response = await fetch('/api/check-auth', { credentials: 'include' // ważne - dołącz ciasteczka }); if (!response.ok) return { authenticated: false }; const data = await response.json(); return data; } catch (error) { return { authenticated: false }; } } document.getElementById('logoutLink')?.addEventListener('click', async (e) => { e.preventDefault(); try { await fetch('/api/logout', { method: 'POST', credentials: 'include' // ważne - dołącz ciasteczka }); window.location.href = '/'; } catch (error) { console.error('Logout error:', error); } });