2025-05-30 18:00:52 +02:00
|
|
|
import { handleApiError, updateAuthUI, setupLogout } from './utils.js';
|
|
|
|
|
2025-05-30 20:02:17 +02:00
|
|
|
document.getElementById('loginForm')?.addEventListener('submit', async (e) => {
|
|
|
|
e.preventDefault();
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-05-30 20:02:17 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-05-30 20:02:17 +02:00
|
|
|
if (response.ok) {
|
|
|
|
window.location.href = '/';
|
|
|
|
} else {
|
|
|
|
alert('Błąd logowania!');
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
2025-05-30 20:02:17 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Błąd:', error);
|
|
|
|
}
|
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
2025-05-30 20:02:17 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2025-05-30 18:00:52 +02:00
|
|
|
});
|