2024-11-18 17:04:10 +01:00
|
|
|
import {showAlert, showConfirm} from './shared_functions.js';
|
2024-11-14 17:43:37 +01:00
|
|
|
|
2024-10-29 21:09:13 +01:00
|
|
|
// IP-Adresse
|
|
|
|
const baseUrl = 'http://localhost:8015';
|
|
|
|
|
|
|
|
// Funktion zum Laden der Benutzerdaten für die normale Webseite
|
|
|
|
async function loadUserData() {
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
if (!token) {
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Nicht autorisiert. Bitte einloggen.');
|
2024-10-29 21:09:13 +01:00
|
|
|
window.location.href = 'index.html';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(`${baseUrl}/api/auth/me`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-18 15:30:08 +01:00
|
|
|
console.log('Response status:', response.status);
|
|
|
|
|
2024-10-29 21:09:13 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Fehler beim Abrufen der Benutzerdaten');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
2024-11-18 15:30:08 +01:00
|
|
|
|
2024-10-29 21:09:13 +01:00
|
|
|
document.getElementById('username').textContent = data.user;
|
|
|
|
document.getElementById('isAdmin').textContent = data.isAdmin ? 'Ja' : 'Nein';
|
|
|
|
|
|
|
|
// Überprüfen, ob der Benutzer Admin ist
|
|
|
|
if (data.isAdmin) {
|
|
|
|
const adminBtn = document.getElementById('adminPermissionsBtn');
|
|
|
|
adminBtn.style.display = 'inline-block'; // Button anzeigen
|
|
|
|
adminBtn.addEventListener('click', () => {
|
|
|
|
window.location.href = 'admin.html'; // Bei Klick weiterleiten
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Sitzung abgelaufen. Bitte erneut einloggen.');
|
2024-10-29 21:09:13 +01:00
|
|
|
localStorage.removeItem('token');
|
|
|
|
window.location.href = 'index.html';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funktion zum Abmelden
|
|
|
|
document.getElementById('logoutBtn').addEventListener('click', () => {
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
window.location.href = 'index.html';
|
|
|
|
});
|
|
|
|
|
|
|
|
function toWelcome() {
|
|
|
|
location.href = 'welcome.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadAdminData() {
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
if (!token) {
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Nicht autorisiert. Bitte einloggen.');
|
2024-10-29 21:09:13 +01:00
|
|
|
window.location.href = 'index.html';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(`${baseUrl}/api/auth/admin`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Zugriff verweigert. Sie sind kein Admin.');
|
2024-10-29 21:09:13 +01:00
|
|
|
window.location.href = 'welcome.html';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
console.log(data.message); // "Willkommen, Admin!"
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Sitzung abgelaufen. Bitte erneut einloggen.');
|
2024-10-29 21:09:13 +01:00
|
|
|
localStorage.removeItem('token');
|
|
|
|
window.location.href = 'index.html';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 17:04:10 +01:00
|
|
|
// Funktion zum Löschen eines Benutzers
|
|
|
|
async function deleteUser(userId, isAdmin) {
|
|
|
|
|
|
|
|
// If the user is an admin, show a warning confirmation
|
|
|
|
if (isAdmin) {
|
|
|
|
const userConfirmed = await showConfirm('Warnung: Möchten Sie wirklich einen Administrator löschen? Dies kann nicht rückgängig gemacht werden.', 'error');
|
|
|
|
if (!userConfirmed) {
|
|
|
|
return; // If the user cancels, do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
try {
|
|
|
|
const response = await fetch(`${baseUrl}/api/auth/user/${userId}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: { 'Authorization': `Bearer ${token}` }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
await loadUserList(); // Liste aktualisieren
|
|
|
|
} else {
|
|
|
|
showAlert('Fehler beim Löschen des Benutzers');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Fehler beim Löschen des Benutzers:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 21:09:13 +01:00
|
|
|
async function loadUserList() {
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
const response = await fetch(`${baseUrl}/api/auth/users`, {
|
|
|
|
headers: { 'Authorization': `Bearer ${token}` }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
const users = await response.json();
|
|
|
|
const userList = document.getElementById('user-list');
|
|
|
|
userList.innerHTML = ''; // Liste leeren
|
|
|
|
|
|
|
|
users.forEach(user => {
|
|
|
|
const userDiv = document.createElement('div');
|
2024-10-29 22:24:31 +01:00
|
|
|
userDiv.style.display = 'flex';
|
|
|
|
userDiv.style.alignItems = 'center';
|
2024-11-18 17:04:10 +01:00
|
|
|
userDiv.setAttribute('id', `user-list-element-${user._id}`); // Give each div a unique ID
|
2024-10-29 21:09:13 +01:00
|
|
|
userDiv.innerHTML = `
|
2024-11-18 17:04:10 +01:00
|
|
|
<p style="flex: 0 0 25%;">Benutzername: ${user.username}</p>
|
|
|
|
<p style="flex: 0 0 25%;">Email: ${user.email}</p>
|
|
|
|
<p style="flex: 0 0 20%;">Admin: ${user.isAdmin}</p>
|
|
|
|
<button class="button-delete" style="flex: 0 0 30%;">Benutzer löschen</button>
|
|
|
|
`;
|
2024-10-29 21:09:13 +01:00
|
|
|
userList.appendChild(userDiv);
|
2024-11-18 17:04:10 +01:00
|
|
|
|
|
|
|
// Add event listener to the specific delete button
|
|
|
|
const deleteButton = userDiv.querySelector(".button-delete");
|
|
|
|
deleteButton.addEventListener("click", function() {
|
|
|
|
deleteUser(user._id, user.isAdmin);
|
|
|
|
});
|
2024-10-29 21:09:13 +01:00
|
|
|
});
|
|
|
|
} else {
|
2024-11-14 17:43:37 +01:00
|
|
|
showAlert('Fehler beim Laden der Benutzerliste');
|
2024-10-29 21:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Überprüfen, ob wir uns auf der Admin-Seite befinden, und dann den Event-Listener hinzufügen
|
|
|
|
const loadUserListBtn = document.getElementById('loadUserListBtn');
|
|
|
|
if (loadUserListBtn) {
|
|
|
|
loadUserListBtn.addEventListener('click', loadUserList); // Benutzerliste laden, wenn der Button geklickt wird
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Abhängig von der Seite entweder Admin- oder Benutzerdaten laden
|
|
|
|
if (window.location.pathname.includes('admin.html')) {
|
2024-11-18 15:30:08 +01:00
|
|
|
// Funktion um auf die Willkommensseite zurückzukommen
|
|
|
|
document.getElementById("welcomeButton").addEventListener("click", toWelcome);
|
2024-11-14 17:43:37 +01:00
|
|
|
await loadAdminData();
|
|
|
|
await loadUserList();
|
2024-10-29 21:09:13 +01:00
|
|
|
} else if (window.location.pathname.includes('welcome.html')) {
|
2024-11-14 17:43:37 +01:00
|
|
|
await loadUserData();
|
2024-10-29 21:09:13 +01:00
|
|
|
}
|
|
|
|
|