diff --git a/public/js/admin.js b/public/js/admin.js
index a19e458..a7b5aaf 100644
--- a/public/js/admin.js
+++ b/public/js/admin.js
@@ -1,21 +1,30 @@
class AdminService {
constructor() {
- if (!window.auth.user?.is_admin) return;
+ // Check if auth exists and user is admin
+ if (!window.auth || !window.auth.user || !window.auth.user.is_admin) return;
this.adminPanel = document.getElementById('admin-panel');
this.closeAdminBtn = document.getElementById('close-admin-btn');
this.addUserBtn = document.getElementById('add-user-btn');
this.usersList = document.getElementById('users-list');
- this.setupEventListeners();
+ if (this.adminPanel && this.closeAdminBtn && this.addUserBtn && this.usersList) {
+ this.setupEventListeners();
+ }
}
setupEventListeners() {
- this.closeAdminBtn.addEventListener('click', () => this.closePanel());
- this.addUserBtn.addEventListener('click', () => this.showAddUserForm());
+ if (this.closeAdminBtn) {
+ this.closeAdminBtn.addEventListener('click', () => this.closePanel());
+ }
+ if (this.addUserBtn) {
+ this.addUserBtn.addEventListener('click', () => this.showAddUserForm());
+ }
}
async loadUsers() {
+ if (!window.auth || !window.auth.getHeaders) return;
+
try {
const response = await fetch('/api/users', {
headers: window.auth.getHeaders()
@@ -28,24 +37,28 @@ class AdminService {
}
renderUsersList(users) {
+ if (!this.usersList || !Array.isArray(users)) return;
+
this.usersList.innerHTML = '';
users.forEach(user => {
+ if (!user || typeof user !== 'object') return;
+
const userElement = document.createElement('div');
userElement.className = 'user-item';
userElement.innerHTML = `
- ${user.username}
+ ${user.username || 'Utilisateur'}
${user.is_admin ? 'Admin' : ''}
${user.id !== 1 ? `
-