2025-05-30 18:00:52 +02:00
|
|
|
export function getAuthHeaders() {
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
return {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleApiError(error, fallbackMsg = 'Wystąpił błąd') {
|
|
|
|
console.error('Error:', error);
|
|
|
|
alert(error.message || fallbackMsg);
|
|
|
|
}
|
|
|
|
|
2025-05-30 20:02:17 +02:00
|
|
|
async function updateAuthUI() {
|
|
|
|
try {
|
|
|
|
const response = await fetch('/api/check-auth', {
|
|
|
|
credentials: 'include'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) return;
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
const authContainers = document.querySelectorAll('.auth-links');
|
|
|
|
|
|
|
|
authContainers.forEach(container => {
|
|
|
|
const anonymousLinks = container.querySelector('.anonymous-links');
|
|
|
|
const userLinks = container.querySelector('.user-links');
|
|
|
|
|
|
|
|
if (data.authenticated) {
|
|
|
|
if (anonymousLinks) anonymousLinks.style.display = 'none';
|
|
|
|
if (userLinks) userLinks.style.display = 'flex';
|
|
|
|
} else {
|
|
|
|
if (anonymousLinks) anonymousLinks.style.display = 'flex';
|
|
|
|
if (userLinks) userLinks.style.display = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Błąd aktualizacji UI:', error);
|
|
|
|
}
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setupLogout() {
|
|
|
|
document.getElementById('logoutLink')?.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
window.location.href = '/';
|
|
|
|
});
|
|
|
|
}
|
2025-05-30 20:02:17 +02:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', updateAuthUI);
|