668 lines
17 KiB
HTML
668 lines
17 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Baby Monitor - Admin</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
font-family:
|
||
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
|
||
Cantarell, sans-serif;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
min-height: 100vh;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
padding: 20px;
|
||
}
|
||
|
||
.container {
|
||
background: white;
|
||
border-radius: 12px;
|
||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
|
||
padding: 40px;
|
||
max-width: 600px;
|
||
width: 100%;
|
||
}
|
||
|
||
h1 {
|
||
color: #333;
|
||
margin-bottom: 10px;
|
||
font-size: 28px;
|
||
}
|
||
|
||
.subtitle {
|
||
color: #666;
|
||
margin-bottom: 30px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.section {
|
||
margin-bottom: 30px;
|
||
}
|
||
|
||
.section h2 {
|
||
color: #444;
|
||
font-size: 18px;
|
||
margin-bottom: 15px;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.section h2::before {
|
||
content: "🔗";
|
||
margin-right: 8px;
|
||
}
|
||
|
||
.button {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
border: none;
|
||
padding: 12px 24px;
|
||
border-radius: 6px;
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
transition:
|
||
transform 0.2s,
|
||
box-shadow 0.2s;
|
||
width: 100%;
|
||
}
|
||
|
||
.button:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||
}
|
||
|
||
.button:active {
|
||
transform: translateY(0);
|
||
}
|
||
|
||
.button:disabled {
|
||
opacity: 0.6;
|
||
cursor: not-allowed;
|
||
transform: none;
|
||
}
|
||
|
||
.link-container {
|
||
display: none;
|
||
margin-top: 20px;
|
||
padding: 15px;
|
||
background: #f8f9fa;
|
||
border-radius: 8px;
|
||
border: 2px solid #667eea;
|
||
}
|
||
|
||
.link-container.show {
|
||
display: block;
|
||
}
|
||
|
||
.link-label {
|
||
font-size: 12px;
|
||
color: #666;
|
||
margin-bottom: 8px;
|
||
font-weight: 600;
|
||
text-transform: uppercase;
|
||
}
|
||
|
||
.link-display {
|
||
display: flex;
|
||
gap: 10px;
|
||
align-items: center;
|
||
}
|
||
|
||
.link-text {
|
||
flex: 1;
|
||
padding: 10px;
|
||
background: white;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
font-family: "Courier New", monospace;
|
||
font-size: 13px;
|
||
word-break: break-all;
|
||
color: #333;
|
||
}
|
||
|
||
.copy-button {
|
||
padding: 10px 16px;
|
||
background: #28a745;
|
||
color: white;
|
||
border: none;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
font-weight: 600;
|
||
transition: background 0.2s;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.copy-button:hover {
|
||
background: #218838;
|
||
}
|
||
|
||
.copy-button.copied {
|
||
background: #155724;
|
||
}
|
||
|
||
.info-box {
|
||
background: #e7f3ff;
|
||
border-left: 4px solid #2196f3;
|
||
padding: 12px;
|
||
border-radius: 4px;
|
||
margin-top: 15px;
|
||
font-size: 14px;
|
||
color: #555;
|
||
}
|
||
|
||
.error {
|
||
background: #ffebee;
|
||
border-left: 4px solid #f44336;
|
||
color: #c62828;
|
||
padding: 12px;
|
||
border-radius: 4px;
|
||
margin-top: 15px;
|
||
display: none;
|
||
}
|
||
|
||
.error.show {
|
||
display: block;
|
||
}
|
||
|
||
.logout-button {
|
||
background: #dc3545;
|
||
padding: 8px 16px;
|
||
font-size: 14px;
|
||
margin-top: 20px;
|
||
}
|
||
|
||
.logout-button:hover {
|
||
background: #c82333;
|
||
box-shadow: 0 4px 12px rgba(220, 53, 69, 0.4);
|
||
}
|
||
|
||
.user-management h2::before {
|
||
content: "👥";
|
||
}
|
||
|
||
.search-box {
|
||
width: 100%;
|
||
padding: 10px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
font-size: 14px;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.user-list {
|
||
max-height: 400px;
|
||
overflow-y: auto;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
}
|
||
|
||
.user-item {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 12px;
|
||
border-bottom: 1px solid #eee;
|
||
background: white;
|
||
}
|
||
|
||
.user-item:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
.user-item:hover {
|
||
background: #f8f9fa;
|
||
}
|
||
|
||
.user-info {
|
||
flex: 1;
|
||
}
|
||
|
||
.user-name {
|
||
font-weight: 600;
|
||
color: #333;
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.user-meta {
|
||
font-size: 12px;
|
||
color: #666;
|
||
}
|
||
|
||
.admin-badge {
|
||
display: inline-block;
|
||
padding: 2px 8px;
|
||
background: #667eea;
|
||
color: white;
|
||
border-radius: 3px;
|
||
font-size: 11px;
|
||
font-weight: 600;
|
||
margin-left: 8px;
|
||
}
|
||
|
||
.delete-user-btn {
|
||
padding: 6px 12px;
|
||
background: #dc3545;
|
||
color: white;
|
||
border: none;
|
||
border-radius: 4px;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
}
|
||
|
||
.delete-user-btn:hover {
|
||
background: #c82333;
|
||
}
|
||
|
||
.delete-user-btn:disabled {
|
||
background: #ccc;
|
||
cursor: not-allowed;
|
||
}
|
||
|
||
.modal {
|
||
display: none;
|
||
position: fixed;
|
||
top: 0;
|
||
left: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
background: rgba(0, 0, 0, 0.5);
|
||
z-index: 1000;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
|
||
.modal.show {
|
||
display: flex;
|
||
}
|
||
|
||
.modal-content {
|
||
background: white;
|
||
padding: 30px;
|
||
border-radius: 8px;
|
||
max-width: 500px;
|
||
width: 90%;
|
||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
|
||
}
|
||
|
||
.modal h3 {
|
||
color: #333;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.modal p {
|
||
color: #666;
|
||
margin-bottom: 20px;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.modal-buttons {
|
||
display: flex;
|
||
gap: 10px;
|
||
justify-content: flex-end;
|
||
}
|
||
|
||
.modal-button {
|
||
padding: 10px 20px;
|
||
border: none;
|
||
border-radius: 4px;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
}
|
||
|
||
.modal-button.cancel {
|
||
background: #6c757d;
|
||
color: white;
|
||
}
|
||
|
||
.modal-button.cancel:hover {
|
||
background: #5a6268;
|
||
}
|
||
|
||
.modal-button.confirm {
|
||
background: #dc3545;
|
||
color: white;
|
||
}
|
||
|
||
.modal-button.confirm:hover {
|
||
background: #c82333;
|
||
}
|
||
|
||
.empty-state {
|
||
text-align: center;
|
||
padding: 40px 20px;
|
||
color: #999;
|
||
}
|
||
|
||
.loading {
|
||
text-align: center;
|
||
padding: 20px;
|
||
color: #666;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<h1>👨💼 Admin Dashboard</h1>
|
||
<p class="subtitle">Manage user invitations and settings</p>
|
||
|
||
<div class="section">
|
||
<h2>Generate Invitation Link</h2>
|
||
<p style="color: #666; margin-bottom: 15px; font-size: 14px">
|
||
Create a secure one-time link to invite a new user to register.
|
||
</p>
|
||
<button class="button" id="generateBtn" onclick="generateLink()">
|
||
Generate New Invitation Link
|
||
</button>
|
||
|
||
<div id="linkContainer" class="link-container">
|
||
<div class="link-label">Invitation Link</div>
|
||
<div class="link-display">
|
||
<div class="link-text" id="linkText"></div>
|
||
<button class="copy-button" id="copyBtn" onclick="copyLink()">
|
||
Copy
|
||
</button>
|
||
</div>
|
||
<div class="info-box">
|
||
ℹ️ This link expires in 24 hours and can only be used once. Share it
|
||
securely with the intended user.
|
||
</div>
|
||
</div>
|
||
|
||
<div id="error" class="error"></div>
|
||
</div>
|
||
|
||
<div class="section user-management">
|
||
<h2>User Management</h2>
|
||
<p style="color: #666; margin-bottom: 15px; font-size: 14px">
|
||
View and manage all users in the system.
|
||
</p>
|
||
|
||
<input
|
||
type="text"
|
||
id="userSearch"
|
||
class="search-box"
|
||
placeholder="Search users by username..."
|
||
oninput="filterUsers()"
|
||
/>
|
||
|
||
<div id="userListContainer" class="loading">Loading users...</div>
|
||
</div>
|
||
|
||
<button class="button logout-button" onclick="logout()">Logout</button>
|
||
</div>
|
||
|
||
<!-- Delete Confirmation Modal -->
|
||
<div class="modal" id="deleteModal">
|
||
<div class="modal-content">
|
||
<h3>⚠️ Confirm User Deletion</h3>
|
||
<p id="deleteMessage"></p>
|
||
<p style="color: #dc3545; font-weight: 600">
|
||
This action cannot be undone. All associated children and their logs
|
||
(feedings, diapers, sleep) will be permanently deleted.
|
||
</p>
|
||
<div class="modal-buttons">
|
||
<button class="modal-button cancel" onclick="closeDeleteModal()">
|
||
Cancel
|
||
</button>
|
||
<button class="modal-button confirm" onclick="confirmDelete()">
|
||
Delete User
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
// Check if user is authenticated and is admin
|
||
const token = localStorage.getItem("access_token");
|
||
if (!token) {
|
||
window.location.href = "/login.html";
|
||
}
|
||
|
||
let allUsers = [];
|
||
let userToDelete = null;
|
||
|
||
// Verify user is admin and load data
|
||
fetch("/api/me", {
|
||
headers: {
|
||
Authorization: `Bearer ${token}`,
|
||
},
|
||
})
|
||
.then((response) => response.json())
|
||
.then((user) => {
|
||
if (!user.is_admin) {
|
||
alert("Access denied. Admin privileges required.");
|
||
window.location.href = "/";
|
||
}
|
||
// Load users after verifying admin status
|
||
loadUsers();
|
||
})
|
||
.catch((error) => {
|
||
console.error("Error verifying admin status:", error);
|
||
window.location.href = "/login.html";
|
||
});
|
||
|
||
async function loadUsers() {
|
||
const container = document.getElementById("userListContainer");
|
||
|
||
try {
|
||
const response = await fetch("/api/admin/users", {
|
||
headers: {
|
||
Authorization: `Bearer ${token}`,
|
||
},
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error("Failed to load users");
|
||
}
|
||
|
||
allUsers = await response.json();
|
||
renderUsers(allUsers);
|
||
} catch (error) {
|
||
console.error("Error loading users:", error);
|
||
container.innerHTML = `
|
||
<div class="empty-state">
|
||
❌ Failed to load users: ${error.message}
|
||
</div>
|
||
`;
|
||
}
|
||
}
|
||
|
||
function renderUsers(users) {
|
||
const container = document.getElementById("userListContainer");
|
||
|
||
if (users.length === 0) {
|
||
container.innerHTML = `
|
||
<div class="empty-state">
|
||
No users found.
|
||
</div>
|
||
`;
|
||
return;
|
||
}
|
||
|
||
container.className = "user-list";
|
||
container.innerHTML = users
|
||
.map(
|
||
(user) => `
|
||
<div class="user-item">
|
||
<div class="user-info">
|
||
<div class="user-name">
|
||
${user.username}
|
||
${user.is_admin ? '<span class="admin-badge">ADMIN</span>' : ""}
|
||
</div>
|
||
<div class="user-meta">
|
||
ID: ${user.id} • Created: ${new Date(user.created_at).toLocaleDateString()}
|
||
</div>
|
||
</div>
|
||
<button
|
||
class="delete-user-btn"
|
||
onclick="showDeleteModal(${user.id}, '${user.username}', ${user.is_admin})"
|
||
${user.is_admin ? 'disabled title="Cannot delete admin users"' : ""}
|
||
>
|
||
Delete
|
||
</button>
|
||
</div>
|
||
`,
|
||
)
|
||
.join("");
|
||
}
|
||
|
||
function filterUsers() {
|
||
const searchTerm = document
|
||
.getElementById("userSearch")
|
||
.value.toLowerCase();
|
||
const filtered = allUsers.filter((user) =>
|
||
user.username.toLowerCase().includes(searchTerm),
|
||
);
|
||
renderUsers(filtered);
|
||
}
|
||
|
||
function showDeleteModal(userId, username, isAdmin) {
|
||
if (isAdmin) {
|
||
return; // Don't allow deleting admin users
|
||
}
|
||
|
||
userToDelete = { id: userId, username };
|
||
const modal = document.getElementById("deleteModal");
|
||
const message = document.getElementById("deleteMessage");
|
||
|
||
message.textContent = `Are you sure you want to delete user "${username}"?`;
|
||
modal.classList.add("show");
|
||
}
|
||
|
||
function closeDeleteModal() {
|
||
const modal = document.getElementById("deleteModal");
|
||
modal.classList.remove("show");
|
||
userToDelete = null;
|
||
}
|
||
|
||
async function confirmDelete() {
|
||
if (!userToDelete) return;
|
||
|
||
const modal = document.getElementById("deleteModal");
|
||
const confirmBtn = modal.querySelector(".modal-button.confirm");
|
||
|
||
confirmBtn.disabled = true;
|
||
confirmBtn.textContent = "Deleting...";
|
||
|
||
try {
|
||
const response = await fetch(`/api/admin/users/${userToDelete.id}`, {
|
||
method: "DELETE",
|
||
headers: {
|
||
Authorization: `Bearer ${token}`,
|
||
},
|
||
});
|
||
|
||
if (!response.ok) {
|
||
const error = await response.json();
|
||
throw new Error(error.detail || "Failed to delete user");
|
||
}
|
||
|
||
const result = await response.json();
|
||
|
||
// Show success message
|
||
alert(
|
||
`✓ ${result.message}\n\n` +
|
||
`Deleted:\n` +
|
||
`• ${result.deleted_children.length} children\n` +
|
||
`• ${result.deleted_feedings} feedings\n` +
|
||
`• ${result.deleted_diapers} diaper changes\n` +
|
||
`• ${result.deleted_sleeps} sleep sessions`,
|
||
);
|
||
|
||
// Reload user list
|
||
await loadUsers();
|
||
closeDeleteModal();
|
||
} catch (error) {
|
||
console.error("Error deleting user:", error);
|
||
alert(`Failed to delete user: ${error.message}`);
|
||
} finally {
|
||
confirmBtn.disabled = false;
|
||
confirmBtn.textContent = "Delete User";
|
||
}
|
||
}
|
||
|
||
async function generateLink() {
|
||
const btn = document.getElementById("generateBtn");
|
||
const linkContainer = document.getElementById("linkContainer");
|
||
const linkText = document.getElementById("linkText");
|
||
const errorDiv = document.getElementById("error");
|
||
|
||
// Disable button and show loading state
|
||
btn.disabled = true;
|
||
btn.textContent = "Generating...";
|
||
errorDiv.classList.remove("show");
|
||
|
||
try {
|
||
const response = await fetch("/api/admin/generate-invitation", {
|
||
method: "POST",
|
||
headers: {
|
||
Authorization: `Bearer ${token}`,
|
||
"Content-Type": "application/json",
|
||
},
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`Failed to generate link: ${response.statusText}`);
|
||
}
|
||
|
||
const data = await response.json();
|
||
|
||
// Build the full URL
|
||
const baseUrl = window.location.origin;
|
||
const inviteUrl = `${baseUrl}/register.html?token=${data.token}`;
|
||
|
||
// Display the link
|
||
linkText.textContent = inviteUrl;
|
||
linkContainer.classList.add("show");
|
||
} catch (error) {
|
||
errorDiv.textContent = `Error: ${error.message}`;
|
||
errorDiv.classList.add("show");
|
||
} finally {
|
||
btn.disabled = false;
|
||
btn.textContent = "Generate New Invitation Link";
|
||
}
|
||
}
|
||
|
||
function copyLink() {
|
||
const linkText = document.getElementById("linkText").textContent;
|
||
const copyBtn = document.getElementById("copyBtn");
|
||
|
||
navigator.clipboard
|
||
.writeText(linkText)
|
||
.then(() => {
|
||
// Show success state
|
||
copyBtn.textContent = "✓ Copied!";
|
||
copyBtn.classList.add("copied");
|
||
|
||
// Reset after 2 seconds
|
||
setTimeout(() => {
|
||
copyBtn.textContent = "Copy";
|
||
copyBtn.classList.remove("copied");
|
||
}, 2000);
|
||
})
|
||
.catch((err) => {
|
||
alert("Failed to copy link: " + err);
|
||
});
|
||
}
|
||
|
||
function logout() {
|
||
localStorage.removeItem("access_token");
|
||
window.location.href = "/login.html";
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|