first commit

This commit is contained in:
Ashim Kumar
2026-02-20 13:53:36 +06:00
commit 8e02b9ad09
35 changed files with 11059 additions and 0 deletions

679
templates/admin.html Normal file
View File

@@ -0,0 +1,679 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - Audiobook Maker Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
:root {
--primary-color: #4f46e5;
--primary-hover: #4338ca;
--success-color: #10b981;
--danger-color: #ef4444;
--warning-color: #f59e0b;
--bg-primary: #f8fafc;
--text-primary: #1e293b;
--text-secondary: #64748b;
--text-muted: #94a3b8;
--border-color: #e2e8f0;
}
* { box-sizing: border-box; }
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
background-color: var(--bg-primary);
color: var(--text-primary);
margin: 0;
}
.admin-header {
background: linear-gradient(135deg, var(--primary-color) 0%, #7c3aed 100%);
color: white;
padding: 16px 24px;
display: flex;
justify-content: space-between;
align-items: center;
}
.admin-header h1 {
font-size: 1.25rem;
font-weight: 700;
margin: 0;
display: flex;
align-items: center;
gap: 10px;
}
.admin-header-actions {
display: flex;
gap: 10px;
align-items: center;
}
.admin-header-actions .btn {
border-radius: 8px;
font-weight: 500;
font-size: 0.85rem;
}
.btn-header-light {
background: rgba(255,255,255,0.15);
border: 1.5px solid rgba(255,255,255,0.4);
color: white;
}
.btn-header-light:hover {
background: rgba(255,255,255,0.25);
color: white;
}
.admin-container {
max-width: 900px;
margin: 32px auto;
padding: 0 24px;
}
.admin-card {
background: white;
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
border: 1px solid var(--border-color);
overflow: hidden;
}
.admin-card-header {
padding: 20px 24px;
border-bottom: 1px solid var(--border-color);
display: flex;
justify-content: space-between;
align-items: center;
}
.admin-card-header h2 {
font-size: 1.1rem;
font-weight: 700;
margin: 0;
display: flex;
align-items: center;
gap: 8px;
}
.user-count-badge {
background: var(--bg-primary);
color: var(--text-secondary);
font-size: 0.75rem;
font-weight: 600;
padding: 2px 10px;
border-radius: 12px;
border: 1px solid var(--border-color);
}
.user-table {
width: 100%;
border-collapse: collapse;
}
.user-table th {
background: var(--bg-primary);
padding: 12px 20px;
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--text-secondary);
text-align: left;
border-bottom: 1px solid var(--border-color);
}
.user-table td {
padding: 14px 20px;
border-bottom: 1px solid #f1f5f9;
font-size: 0.88rem;
vertical-align: middle;
}
.user-table tr:last-child td {
border-bottom: none;
}
.user-table tr:hover td {
background: #fafbfd;
}
.role-badge {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 10px;
border-radius: 12px;
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.3px;
}
.role-badge.admin {
background: #ede9fe;
color: #5b21b6;
}
.role-badge.user {
background: #e0f2fe;
color: #0369a1;
}
.status-badge {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 0.8rem;
font-weight: 500;
}
.status-badge .status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.status-badge.active .status-dot { background: var(--success-color); }
.status-badge.active { color: #065f46; }
.status-badge.disabled .status-dot { background: var(--danger-color); }
.status-badge.disabled { color: #991b1b; }
.user-actions .btn {
padding: 4px 10px;
font-size: 0.78rem;
border-radius: 6px;
}
.date-text {
font-size: 0.8rem;
color: var(--text-muted);
}
.empty-state {
text-align: center;
padding: 60px 20px;
color: var(--text-muted);
}
.empty-state i {
font-size: 3rem;
margin-bottom: 12px;
display: block;
}
/* Modal tweaks */
.modal-content {
border: none;
border-radius: 16px;
box-shadow: 0 20px 50px rgba(0,0,0,0.2);
}
.modal-header {
border-bottom: 1px solid var(--border-color);
padding: 20px 24px;
}
.modal-body { padding: 24px; }
.modal-footer { border-top: 1px solid var(--border-color); padding: 16px 24px; }
.form-label {
font-weight: 600;
font-size: 0.85rem;
color: #374151;
}
.form-control, .form-select {
padding: 10px 14px;
border: 2px solid var(--border-color);
border-radius: 8px;
font-size: 0.9rem;
}
.form-control:focus, .form-select:focus {
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
}
.toast-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
}
@media (max-width: 768px) {
.admin-header {
flex-direction: column;
gap: 12px;
text-align: center;
}
.admin-card-header {
flex-direction: column;
gap: 12px;
}
.user-table {
font-size: 0.82rem;
}
.user-table th, .user-table td {
padding: 10px 12px;
}
}
</style>
</head>
<body>
<!-- Header -->
<header class="admin-header">
<h1>
<i class="bi bi-shield-lock"></i>
Admin Dashboard
</h1>
<div class="admin-header-actions">
<span class="text-white-50" id="currentUserLabel"></span>
<a href="/" class="btn btn-header-light btn-sm">
<i class="bi bi-arrow-left me-1"></i> Back to App
</a>
<button class="btn btn-header-light btn-sm" onclick="handleLogout()">
<i class="bi bi-box-arrow-right me-1"></i> Logout
</button>
</div>
</header>
<!-- Main Content -->
<div class="admin-container">
<div class="admin-card">
<div class="admin-card-header">
<h2>
<i class="bi bi-people"></i>
User Management
<span class="user-count-badge" id="userCountBadge">0 users</span>
</h2>
<button class="btn btn-primary btn-sm" onclick="openCreateModal()">
<i class="bi bi-person-plus me-1"></i> Create User
</button>
</div>
<div id="userTableContainer">
<div class="empty-state">
<div class="spinner-border text-primary" role="status"></div>
<p class="mt-2">Loading users...</p>
</div>
</div>
</div>
</div>
<!-- Toast Container -->
<div class="toast-container" id="toastContainer"></div>
<!-- Create/Edit User Modal -->
<div class="modal fade" id="userModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="userModalTitle">
<i class="bi bi-person-plus me-2"></i>Create New User
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="editUserId">
<div class="mb-3">
<label for="modalUsername" class="form-label">Username</label>
<input type="text" class="form-control" id="modalUsername"
placeholder="Enter username" minlength="3" required>
</div>
<div class="mb-3">
<label for="modalPassword" class="form-label">
Password
<span class="text-muted fw-normal" id="passwordHint" style="display:none;">(leave blank to keep current)</span>
</label>
<input type="text" class="form-control" id="modalPassword"
placeholder="Enter password" minlength="4">
</div>
<div class="mb-3">
<label for="modalRole" class="form-label">Role</label>
<select class="form-select" id="modalRole">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="mb-3" id="activeField" style="display:none;">
<label class="form-label">Status</label>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="modalActive" checked>
<label class="form-check-label" for="modalActive">Account Active</label>
</div>
</div>
<div class="alert alert-danger" id="modalError" style="display:none;"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="modalSaveBtn" onclick="saveUser()">
<i class="bi bi-check-lg me-1"></i> Create User
</button>
</div>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><i class="bi bi-exclamation-triangle text-danger me-2"></i>Delete User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete <strong id="deleteUserName"></strong>?</p>
<p class="text-muted small mb-0">This action cannot be undone.</p>
<input type="hidden" id="deleteUserId">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger btn-sm" onclick="confirmDelete()">
<i class="bi bi-trash me-1"></i> Delete
</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
let userModal, deleteModal;
let currentUsers = [];
document.addEventListener('DOMContentLoaded', function() {
userModal = new bootstrap.Modal(document.getElementById('userModal'));
deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
loadCurrentUser();
loadUsers();
});
async function loadCurrentUser() {
try {
const resp = await fetch('/api/auth/me');
const data = await resp.json();
if (data.user) {
document.getElementById('currentUserLabel').textContent =
`Logged in as ${data.user.username}`;
}
} catch(e) { /* ignore */ }
}
async function loadUsers() {
try {
const resp = await fetch('/api/admin/users');
const data = await resp.json();
currentUsers = data.users || [];
renderUserTable(currentUsers);
} catch(e) {
document.getElementById('userTableContainer').innerHTML =
'<div class="empty-state"><i class="bi bi-exclamation-circle"></i><p>Failed to load users</p></div>';
}
}
function renderUserTable(users) {
const container = document.getElementById('userTableContainer');
const badge = document.getElementById('userCountBadge');
badge.textContent = `${users.length} user${users.length !== 1 ? 's' : ''}`;
if (users.length === 0) {
container.innerHTML = '<div class="empty-state"><i class="bi bi-people"></i><p>No users found</p></div>';
return;
}
let html = `<table class="user-table">
<thead>
<tr>
<th>User</th>
<th>Role</th>
<th>Status</th>
<th>Last Login</th>
<th>Created</th>
<th style="text-align:right;">Actions</th>
</tr>
</thead>
<tbody>`;
for (const user of users) {
const statusClass = user.is_active ? 'active' : 'disabled';
const statusText = user.is_active ? 'Active' : 'Disabled';
const lastLogin = user.last_login ? formatDate(user.last_login) : '<span class="text-muted">Never</span>';
html += `<tr>
<td>
<strong>${escapeHtml(user.username)}</strong>
</td>
<td>
<span class="role-badge ${user.role}">
<i class="bi bi-${user.role === 'admin' ? 'shield-check' : 'person'}"></i>
${user.role}
</span>
</td>
<td>
<span class="status-badge ${statusClass}">
<span class="status-dot"></span>
${statusText}
</span>
</td>
<td class="date-text">${lastLogin}</td>
<td class="date-text">${formatDate(user.created_at)}</td>
<td style="text-align:right;">
<div class="user-actions d-flex gap-1 justify-content-end">
<button class="btn btn-outline-primary btn-sm" onclick="openEditModal(${user.id})" title="Edit user">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-outline-danger btn-sm" onclick="openDeleteModal(${user.id}, '${escapeHtml(user.username)}')" title="Delete user">
<i class="bi bi-trash"></i>
</button>
</div>
</td>
</tr>`;
}
html += '</tbody></table>';
container.innerHTML = html;
}
// --- Create / Edit ---
function openCreateModal() {
document.getElementById('userModalTitle').innerHTML = '<i class="bi bi-person-plus me-2"></i>Create New User';
document.getElementById('editUserId').value = '';
document.getElementById('modalUsername').value = '';
document.getElementById('modalPassword').value = '';
document.getElementById('modalPassword').required = true;
document.getElementById('modalPassword').placeholder = 'Enter password';
document.getElementById('passwordHint').style.display = 'none';
document.getElementById('modalRole').value = 'user';
document.getElementById('modalActive').checked = true;
document.getElementById('activeField').style.display = 'none';
document.getElementById('modalSaveBtn').innerHTML = '<i class="bi bi-check-lg me-1"></i> Create User';
document.getElementById('modalError').style.display = 'none';
userModal.show();
}
function openEditModal(userId) {
const user = currentUsers.find(u => u.id === userId);
if (!user) return;
document.getElementById('userModalTitle').innerHTML = '<i class="bi bi-pencil me-2"></i>Edit User';
document.getElementById('editUserId').value = userId;
document.getElementById('modalUsername').value = user.username;
document.getElementById('modalPassword').value = '';
document.getElementById('modalPassword').required = false;
document.getElementById('modalPassword').placeholder = 'Leave blank to keep current';
document.getElementById('passwordHint').style.display = 'inline';
document.getElementById('modalRole').value = user.role;
document.getElementById('modalActive').checked = user.is_active;
document.getElementById('activeField').style.display = 'block';
document.getElementById('modalSaveBtn').innerHTML = '<i class="bi bi-check-lg me-1"></i> Save Changes';
document.getElementById('modalError').style.display = 'none';
userModal.show();
}
async function saveUser() {
const userId = document.getElementById('editUserId').value;
const username = document.getElementById('modalUsername').value.trim();
const password = document.getElementById('modalPassword').value;
const role = document.getElementById('modalRole').value;
const isActive = document.getElementById('modalActive').checked;
const errorDiv = document.getElementById('modalError');
errorDiv.style.display = 'none';
if (!username || username.length < 3) {
errorDiv.textContent = 'Username must be at least 3 characters';
errorDiv.style.display = 'block';
return;
}
if (!userId && (!password || password.length < 4)) {
errorDiv.textContent = 'Password must be at least 4 characters';
errorDiv.style.display = 'block';
return;
}
if (userId && password && password.length < 4) {
errorDiv.textContent = 'Password must be at least 4 characters';
errorDiv.style.display = 'block';
return;
}
try {
let url, method, body;
if (userId) {
url = `/api/admin/users/${userId}`;
method = 'PUT';
body = { username, role, is_active: isActive };
if (password) body.password = password;
} else {
url = '/api/admin/users';
method = 'POST';
body = { username, password, role };
}
const resp = await fetch(url, {
method: method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
const data = await resp.json();
if (data.error) {
errorDiv.textContent = data.error;
errorDiv.style.display = 'block';
return;
}
userModal.hide();
showToast(data.message || 'User saved successfully', 'success');
loadUsers();
} catch(e) {
errorDiv.textContent = 'Network error. Please try again.';
errorDiv.style.display = 'block';
}
}
// --- Delete ---
function openDeleteModal(userId, username) {
document.getElementById('deleteUserId').value = userId;
document.getElementById('deleteUserName').textContent = username;
deleteModal.show();
}
async function confirmDelete() {
const userId = document.getElementById('deleteUserId').value;
try {
const resp = await fetch(`/api/admin/users/${userId}`, { method: 'DELETE' });
const data = await resp.json();
deleteModal.hide();
if (data.error) {
showToast(data.error, 'error');
return;
}
showToast(data.message || 'User deleted', 'success');
loadUsers();
} catch(e) {
deleteModal.hide();
showToast('Failed to delete user', 'error');
}
}
// --- Logout ---
async function handleLogout() {
try {
await fetch('/api/auth/logout', { method: 'POST' });
} catch(e) { /* ignore */ }
window.location.href = '/login';
}
// --- Utilities ---
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function formatDate(dateStr) {
if (!dateStr) return '';
const d = new Date(dateStr);
return d.toLocaleDateString() + ' ' + d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
function showToast(message, type = 'info') {
const container = document.getElementById('toastContainer');
const colors = { success: 'success', error: 'danger', info: 'primary' };
const icons = { success: 'check-circle', error: 'exclamation-circle', info: 'info-circle' };
const toast = document.createElement('div');
toast.className = `alert alert-${colors[type] || 'info'} d-flex align-items-center gap-2`;
toast.style.cssText = 'min-width:280px; animation: slideIn 0.3s ease; margin-bottom: 8px;';
toast.innerHTML = `<i class="bi bi-${icons[type] || 'info-circle'}"></i> ${escapeHtml(message)}`;
container.appendChild(toast);
setTimeout(() => {
toast.style.animation = 'slideOut 0.3s ease';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// Inject animation styles
const s = document.createElement('style');
s.textContent = `
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes slideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(100%); opacity: 0; } }
`;
document.head.appendChild(s);
</script>
</body>
</html>