119 lines
4.8 KiB
HTML
119 lines
4.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Audit Log — VitFit</title>
|
|
<link rel="manifest" href="/manifest.json">
|
|
<link rel="stylesheet" href="/styles/base.css">
|
|
<link rel="stylesheet" href="/styles/layout.css">
|
|
<link rel="stylesheet" href="/styles/components.css">
|
|
</head>
|
|
<body>
|
|
<header class="banner">
|
|
<button class="banner-hamburger" id="nav-hamburger" aria-label="Menu">☰</button>
|
|
<img id="banner-logo" alt="Logo" style="display:none;height:36px;">
|
|
<span id="app-name" class="banner-title">VitFit</span>
|
|
</header>
|
|
<div class="drawer-scrim" id="drawer-scrim"></div>
|
|
<nav class="nav-drawer" id="nav-drawer"><ul class="nav-list" id="nav-list"></ul></nav>
|
|
|
|
<main class="main-content">
|
|
<div class="page-header">
|
|
<h1 class="page-title">Audit Log</h1>
|
|
<a id="export-btn" href="/api/v1/audit-logs/export" class="btn btn-ghost">Export CSV</a>
|
|
</div>
|
|
|
|
<div class="filter-bar">
|
|
<input class="form-input" type="text" id="action-filter" placeholder="Filter by action…" style="max-width:200px;">
|
|
<input class="form-input" type="text" id="entity-filter" placeholder="Entity type…" style="max-width:200px;">
|
|
</div>
|
|
|
|
<div id="log-list">
|
|
<div class="empty-state"><p class="empty-state-text">Loading…</p></div>
|
|
</div>
|
|
|
|
<div id="pagination" class="pagination"></div>
|
|
</main>
|
|
|
|
<div id="session-expired-overlay" class="session-expired-overlay">
|
|
<div class="session-expired-box"><p>Your session has expired.</p><a href="/" class="btn btn-primary">Sign In Again</a></div>
|
|
</div>
|
|
|
|
<script src="/js/core.js"></script>
|
|
<script>
|
|
let _page = 1;
|
|
|
|
async function loadLogs() {
|
|
const el = document.getElementById('log-list');
|
|
const action = document.getElementById('action-filter').value.trim();
|
|
const entity = document.getElementById('entity-filter').value.trim();
|
|
try {
|
|
const res = await Api.get('/api/v1/audit-logs', { page: _page, limit: 50, action: action || undefined, entityType: entity || undefined });
|
|
if (!res.ok) { el.innerHTML = '<div class="empty-state"><p class="empty-state-text">Failed to load.</p></div>'; return; }
|
|
const { data, meta } = await res.json();
|
|
if (!data.length) { el.innerHTML = '<div class="empty-state"><p class="empty-state-text">No log entries.</p></div>'; return; }
|
|
|
|
el.innerHTML = `<table class="table"><thead><tr><th>Time</th><th>User</th><th>Action</th><th>Entity</th><th>ID</th></tr></thead><tbody>
|
|
${data.map(l => `<tr>
|
|
<td style="white-space:nowrap;">${new Date(l.createdAt).toLocaleString()}</td>
|
|
<td>${l.user?.name || l.userId || '—'}</td>
|
|
<td><code>${l.action}</code></td>
|
|
<td>${l.entityType || '—'}</td>
|
|
<td>${l.entityId || '—'}</td>
|
|
</tr>`).join('')}
|
|
</tbody></table>`;
|
|
|
|
renderPagination(meta);
|
|
} catch {
|
|
el.innerHTML = '<div class="empty-state"><p class="empty-state-text">Error loading log.</p></div>';
|
|
}
|
|
}
|
|
|
|
function renderPagination(meta) {
|
|
const el = document.getElementById('pagination');
|
|
if (!meta || meta.totalPages <= 1) { el.innerHTML = ''; return; }
|
|
el.innerHTML = `
|
|
<button class="btn btn-ghost btn-sm" ${_page <= 1 ? 'disabled' : ''} data-action="page" data-page="${_page - 1}">Previous</button>
|
|
<span class="pagination-info">Page ${meta.page} of ${meta.totalPages}</span>
|
|
<button class="btn btn-ghost btn-sm" ${_page >= meta.totalPages ? 'disabled' : ''} data-action="page" data-page="${_page + 1}">Next</button>`;
|
|
}
|
|
|
|
function changePage(p) { _page = p; loadLogs(); }
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
await Page.init(['Owner']);
|
|
|
|
// Add Bearer to export link
|
|
const jwt = Auth.getJwt();
|
|
if (jwt) {
|
|
document.getElementById('export-btn').addEventListener('click', async (e) => {
|
|
e.preventDefault();
|
|
const res = await Api.get('/api/v1/audit-logs/export');
|
|
const blob = await res.blob();
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url; a.download = 'audit-log.csv'; a.click();
|
|
URL.revokeObjectURL(url);
|
|
});
|
|
}
|
|
|
|
document.getElementById('pagination').addEventListener('click', (e) => {
|
|
const btn = e.target.closest('button[data-action="page"]');
|
|
if (btn) changePage(parseInt(btn.dataset.page, 10));
|
|
});
|
|
|
|
loadLogs();
|
|
|
|
let debounce;
|
|
['action-filter', 'entity-filter'].forEach(id => {
|
|
document.getElementById(id).addEventListener('input', () => {
|
|
clearTimeout(debounce);
|
|
debounce = setTimeout(() => { _page = 1; loadLogs(); }, 400);
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|