141 lines
4.3 KiB
JavaScript
141 lines
4.3 KiB
JavaScript
import { setTheme } from './theme.js';
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const loginForm = document.getElementById('loginForm');
|
|
const registerForm = document.getElementById('registerForm');
|
|
const logoutLink = document.getElementById('logoutLink');
|
|
|
|
checkAuthStatus();
|
|
|
|
if (loginForm) {
|
|
loginForm.addEventListener('submit', handleLogin);
|
|
}
|
|
|
|
if (registerForm) {
|
|
registerForm.addEventListener('submit', handleRegister);
|
|
}
|
|
|
|
if (logoutLink) {
|
|
logoutLink.addEventListener('click', handleLogout);
|
|
}
|
|
});
|
|
|
|
async function checkAuthStatus() {
|
|
try {
|
|
const response = await fetch('/api/check-auth', {
|
|
credentials: 'include'
|
|
});
|
|
|
|
const data = await response.json();
|
|
const authLinks = document.querySelector('.auth-links');
|
|
|
|
if (authLinks) {
|
|
const anonymousLinks = authLinks.querySelector('.anonymous-links');
|
|
const userLinks = authLinks.querySelector('.user-links');
|
|
|
|
if (anonymousLinks && userLinks) {
|
|
if (data.authenticated) {
|
|
anonymousLinks.style.display = 'none';
|
|
userLinks.style.display = 'flex';
|
|
} else {
|
|
anonymousLinks.style.display = 'flex';
|
|
userLinks.style.display = 'none';
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Błąd sprawdzania statusu uwierzytelnienia:', error);
|
|
}
|
|
}
|
|
|
|
async function handleLogin(e) {
|
|
e.preventDefault();
|
|
|
|
const email = document.getElementById('loginEmail').value;
|
|
const password = document.getElementById('loginPassword').value;
|
|
|
|
try {
|
|
const response = await fetch('/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ email, haslo: password }),
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
localStorage.setItem('userName', data.imie);
|
|
window.location.href = '/';
|
|
} else {
|
|
const errorData = await response.json();
|
|
alert(`Błąd logowania: ${errorData.message || 'Nieznany błąd'}`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Błąd logowania:', error);
|
|
alert('Wystąpił błąd podczas logowania');
|
|
}
|
|
}
|
|
|
|
async function handleRegister(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({
|
|
email,
|
|
haslo: password,
|
|
imie: name,
|
|
confirm_password: confirmPassword
|
|
}),
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (response.ok) {
|
|
alert('Konto zostało utworzone! Możesz się zalogować.');
|
|
window.location.href = '/login.html';
|
|
} else {
|
|
const errorData = await response.json();
|
|
alert(`Błąd rejestracji: ${errorData.message || 'Nieznany błąd'}`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Błąd rejestracji:', error);
|
|
alert('Wystąpił błąd podczas rejestracji');
|
|
}
|
|
}
|
|
|
|
async function handleLogout(e) {
|
|
e.preventDefault();
|
|
|
|
try {
|
|
const response = await fetch('/logout', {
|
|
method: 'POST',
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (response.ok) {
|
|
localStorage.removeItem('userName');
|
|
window.location.href = '/';
|
|
} else {
|
|
alert('Wystąpił problem podczas wylogowywania');
|
|
}
|
|
} catch (error) {
|
|
console.error('Błąd wylogowywania:', error);
|
|
alert('Wystąpił błąd podczas wylogowywania');
|
|
}
|
|
}
|