29 lines
907 B
JavaScript
29 lines
907 B
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const navHTML = `
|
|
<nav>
|
|
<a href="/">Главная</a>
|
|
<a href="/clients.html">Клиенты</a>
|
|
<a href="/admin.html">Панель управления</a>
|
|
<a id="logoutLink" href="#" style="display:none;">Выход</a>
|
|
</nav>
|
|
`;
|
|
const header = document.querySelector('header');
|
|
if (header) header.innerHTML += navHTML;
|
|
|
|
// Показать/скрыть выход, проверив сессию
|
|
fetch('/api/me')
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
if (data.isAdmin) {
|
|
document.getElementById('logoutLink').style.display = 'inline';
|
|
}
|
|
});
|
|
|
|
document.addEventListener('click', (e) => {
|
|
if (e.target.id === 'logoutLink') {
|
|
e.preventDefault();
|
|
fetch('/api/logout', { method: 'POST' })
|
|
.then(() => window.location.href = '/login.html');
|
|
}
|
|
});
|
|
}); |