Fix user addition functionality with improved error handling
This commit is contained in:
1 parent
90d79924f8
commit
027074f43d
4 files changed
+280
-27
No files matched your search
+14
-1
@@ -12,7 +12,20 @@
|
||||
<link href="gamification.css" rel="stylesheet"/>
|
||||
<link href="spaced-repetition.css" rel="stylesheet"/>
|
||||
<!-- Scripts -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/2.4.2/sha.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/2.4.2/sha.js" integrity="sha512-MK4YCs3cv9dRCsEzFP5rehiUXbU0iKdwILVZ2SylXqCFFWOk1F4vGRrGGXc0qbHWM7S6gy4Xv/MGK19vKZbvKA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||
<!-- Fallback for jsSHA library if CDN fails -->
|
||||
<script>
|
||||
window.addEventListener('DOMContentLoaded', function() {
|
||||
if (typeof jsSHA === 'undefined') {
|
||||
console.warn('jsSHA not loaded from CDN, using fallback');
|
||||
const script = document.createElement('script');
|
||||
script.src = 'https://cdn.jsdelivr.net/npm/jssha@3.3.0/dist/sha.js';
|
||||
document.head.appendChild(script);
|
||||
} else {
|
||||
console.log('jsSHA loaded successfully');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script src="js/auth.js"></script>
|
||||
<script src="js/ui.js"></script>
|
||||
<script src="js/themes.js"></script>
|
||||
|
||||
+32
-7
@@ -217,26 +217,51 @@ async function addNewUser() {
|
||||
return;
|
||||
}
|
||||
|
||||
const hash = hashPassword(password);
|
||||
if (hashPreview) hashPreview.textContent = hash;
|
||||
// Check if hashPassword function exists
|
||||
let hash;
|
||||
try {
|
||||
hash = hashPassword(password);
|
||||
console.log("✅ Password hashed successfully");
|
||||
if (hashPreview) hashPreview.textContent = hash;
|
||||
} catch (e) {
|
||||
console.error("❌ Error hashing password:", e);
|
||||
msg.textContent = "Error: Unable to hash password. Check console for details.";
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await fetch('/addUser', {
|
||||
console.log(`🔄 Sending request to add user: ${username} (${role})`);
|
||||
|
||||
const response = await fetch('/addUser', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, hash, role })
|
||||
});
|
||||
|
||||
console.log(`📥 Server response: ${response.status} ${response.statusText}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Server returned ${response.status}: ${errorText}`);
|
||||
}
|
||||
|
||||
const successText = await response.text();
|
||||
console.log(`✅ Success: ${successText}`);
|
||||
msg.textContent = `✅ User "${username}" added to server.`;
|
||||
|
||||
// Clear form
|
||||
document.getElementById("newUsername").value = '';
|
||||
document.getElementById("newPassword").value = '';
|
||||
if (hashPreview) hashPreview.textContent = '[auto]';
|
||||
|
||||
await loadUsers();
|
||||
displayUserDropdown();
|
||||
populateWordUserDropdown();
|
||||
// Refresh user lists
|
||||
await loadUsers();
|
||||
displayUserDropdown();
|
||||
if (typeof populateWordUserDropdown === 'function') {
|
||||
populateWordUserDropdown();
|
||||
}
|
||||
} catch (e) {
|
||||
msg.textContent = "❌ Failed to add user.";
|
||||
msg.textContent = `❌ Failed to add user: ${e.message}`;
|
||||
console.error("Add user error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user