Update project files
This commit is contained in:
+50
-22
@@ -1,44 +1,71 @@
|
||||
const API = location.origin;
|
||||
let token = sessionStorage.getItem('adminToken');
|
||||
function hdr() { return { 'X-Admin-Token': token }; }
|
||||
function hdrJson() { return { 'Content-Type': 'application/json', 'X-Admin-Token': token }; }
|
||||
let token = sessionStorage.getItem('authToken');
|
||||
let currentUser = null;
|
||||
function hdr() { return { 'X-Auth-Token': token }; }
|
||||
function hdrJson() { return { 'Content-Type': 'application/json', 'X-Auth-Token': token }; }
|
||||
function esc(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
|
||||
|
||||
// --- Auth check ---
|
||||
async function checkAuth() {
|
||||
if (!token) { location.href = 'login.html'; return false; }
|
||||
try {
|
||||
const res = await fetch(`${API}/api/stats`, { headers: hdr() });
|
||||
if (!res.ok) { sessionStorage.removeItem('adminToken'); location.href = 'login.html'; return false; }
|
||||
const res = await fetch(`${API}/api/auth/me`, { headers: hdr() });
|
||||
if (!res.ok) { cleanupSession(); return false; }
|
||||
currentUser = await res.json();
|
||||
return true;
|
||||
} catch { sessionStorage.removeItem('adminToken'); location.href = 'login.html'; return false; }
|
||||
} catch { cleanupSession(); return false; }
|
||||
}
|
||||
|
||||
function doLogout() {
|
||||
sessionStorage.removeItem('adminToken');
|
||||
function cleanupSession() {
|
||||
sessionStorage.removeItem('authToken');
|
||||
token = null;
|
||||
currentUser = null;
|
||||
location.href = 'login.html';
|
||||
}
|
||||
|
||||
function isAdmin() { return currentUser && currentUser.role === 'admin'; }
|
||||
|
||||
async function requireAdminPage() {
|
||||
if (!(await checkAuth())) return false;
|
||||
if (!isAdmin()) { location.href = 'journal.html'; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
async function doLogout() {
|
||||
try { await fetch(`${API}/api/auth/logout`, { method: 'POST', headers: hdr() }); } catch {}
|
||||
cleanupSession();
|
||||
}
|
||||
|
||||
// --- Sidebar ---
|
||||
const NAV = [
|
||||
{ page: 'dashboard', label: 'Дашборд' },
|
||||
{ page: 'journal', label: 'Журнал' },
|
||||
{ page: 'trash', label: 'Корзина' },
|
||||
{ page: 'files', label: 'Файлы' },
|
||||
{ page: 'links', label: 'Ссылки' },
|
||||
{ page: 'students', label: 'Ученики' },
|
||||
{ page: 'groups', label: 'Группы' },
|
||||
{ page: 'branches', label: 'Филиалы' },
|
||||
{ page: 'audit', label: 'Аудит' },
|
||||
{ page: 'settings', label: 'Настройки' }
|
||||
];
|
||||
function buildSidebar(active) {
|
||||
const el = document.getElementById('sidebar');
|
||||
if (!el) return;
|
||||
const base = [
|
||||
{ page: 'dashboard', label: 'Дашборд' },
|
||||
{ page: 'journal', label: 'Журнал' },
|
||||
{ page: 'trash', label: 'Корзина' },
|
||||
{ page: 'files', label: 'Файлы' },
|
||||
{ page: 'links', label: 'Ссылки' },
|
||||
{ page: 'students', label: 'Ученики' },
|
||||
{ page: 'groups', label: 'Группы' },
|
||||
];
|
||||
const adminNav = [
|
||||
{ page: 'branches', label: 'Филиалы' },
|
||||
{ page: 'users', label: 'Пользователи' },
|
||||
{ page: 'audit', label: 'Аудит' },
|
||||
{ page: 'settings', label: 'Настройки' },
|
||||
];
|
||||
const nav = [...base, ...(isAdmin() ? adminNav : [])];
|
||||
const userName = currentUser?.name || currentUser?.username || '';
|
||||
const roleLabel = isAdmin() ? 'Администратор' : 'Тутор';
|
||||
el.innerHTML = `
|
||||
<div class="logo">Админ-панель</div>
|
||||
<div class="user-box">
|
||||
<div class="user-name">${esc(userName)}</div>
|
||||
<div class="user-role">${roleLabel}</div>
|
||||
</div>
|
||||
<nav>
|
||||
${NAV.map(n => `<a href="${n.page}.html" class="${n.page === active ? 'active' : ''}">${n.label}</a>`).join('')}
|
||||
${nav.map(n => `<a href="${n.page}.html" class="${n.page === active ? 'active' : ''}">${n.label}</a>`).join('')}
|
||||
</nav>
|
||||
<div class="bottom">
|
||||
<a href="/">На главную</a>
|
||||
@@ -49,6 +76,7 @@ function buildSidebar(active) {
|
||||
// --- Common UI ---
|
||||
function showToast(msg) {
|
||||
const t = document.getElementById('toast');
|
||||
if (!t) return;
|
||||
t.textContent = msg;
|
||||
t.classList.add('show');
|
||||
setTimeout(() => t.classList.remove('show'), 3000);
|
||||
@@ -61,7 +89,7 @@ function showImg(src) {
|
||||
}
|
||||
|
||||
async function fillAutocomplete() {
|
||||
const res = await fetch(`${API}/api/students`);
|
||||
const res = await fetch(`${API}/api/students`, { headers: hdr() });
|
||||
const students = await res.json();
|
||||
const names = students.map(s => s.name);
|
||||
const dlEdit = document.getElementById('studentEditList');
|
||||
|
||||
Reference in New Issue
Block a user