auth additions
This commit is contained in:
168
auth.js
168
auth.js
@@ -32,93 +32,94 @@ export function initAuth() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the logout button.
|
// Set up the logout button.
|
||||||
document.getElementById("logoutBtn").addEventListener("click", function () {
|
document.getElementById("logoutBtn").addEventListener("click", function () {
|
||||||
fetch("logout.php", { method: "POST" })
|
fetch("logout.php", { method: "POST" })
|
||||||
.then(() => window.location.reload(true))
|
.then(() => window.location.reload(true))
|
||||||
.catch(error => console.error("Logout error:", error));
|
.catch(error => console.error("Logout error:", error));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set up Add User functionality.
|
// Set up Add User functionality.
|
||||||
document.getElementById("addUserBtn").addEventListener("click", function () {
|
document.getElementById("addUserBtn").addEventListener("click", function () {
|
||||||
resetUserForm();
|
resetUserForm();
|
||||||
toggleVisibility("addUserModal", true);
|
toggleVisibility("addUserModal", true);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("saveUserBtn").addEventListener("click", function () {
|
document.getElementById("saveUserBtn").addEventListener("click", function () {
|
||||||
const newUsername = document.getElementById("newUsername").value.trim();
|
const newUsername = document.getElementById("newUsername").value.trim();
|
||||||
const newPassword = document.getElementById("newPassword").value.trim();
|
const newPassword = document.getElementById("newPassword").value.trim();
|
||||||
const isAdmin = document.getElementById("isAdmin").checked;
|
const isAdmin = document.getElementById("isAdmin").checked;
|
||||||
if (!newUsername || !newPassword) {
|
if (!newUsername || !newPassword) {
|
||||||
showToast("Username and password are required!");
|
showToast("Username and password are required!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let url = "addUser.php";
|
let url = "addUser.php";
|
||||||
if (window.setupMode) {
|
if (window.setupMode) {
|
||||||
url += "?setup=1";
|
url += "?setup=1";
|
||||||
}
|
}
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username: newUsername, password: newPassword, isAdmin })
|
body: JSON.stringify({ username: newUsername, password: newPassword, isAdmin })
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
showToast("User added successfully!");
|
||||||
|
closeAddUserModal();
|
||||||
|
checkAuthentication();
|
||||||
|
} else {
|
||||||
|
showToast("Error: " + (data.error || "Could not add user"));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.catch(error => console.error("Error adding user:", error));
|
||||||
.then(data => {
|
});
|
||||||
if (data.success) {
|
|
||||||
showToast("User added successfully!");
|
|
||||||
closeAddUserModal();
|
|
||||||
checkAuthentication();
|
|
||||||
} else {
|
|
||||||
showToast("Error: " + (data.error || "Could not add user"));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => console.error("Error adding user:", error));
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById("cancelUserBtn").addEventListener("click", function () {
|
document.getElementById("cancelUserBtn").addEventListener("click", function () {
|
||||||
closeAddUserModal();
|
closeAddUserModal();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set up Remove User functionality.
|
// Set up Remove User functionality.
|
||||||
document.getElementById("removeUserBtn").addEventListener("click", function () {
|
document.getElementById("removeUserBtn").addEventListener("click", function () {
|
||||||
loadUserList();
|
loadUserList();
|
||||||
toggleVisibility("removeUserModal", true);
|
toggleVisibility("removeUserModal", true);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("deleteUserBtn").addEventListener("click", function () {
|
document.getElementById("deleteUserBtn").addEventListener("click", function () {
|
||||||
const selectElem = document.getElementById("removeUsernameSelect");
|
const selectElem = document.getElementById("removeUsernameSelect");
|
||||||
const usernameToRemove = selectElem.value;
|
const usernameToRemove = selectElem.value;
|
||||||
if (!usernameToRemove) {
|
if (!usernameToRemove) {
|
||||||
showToast("Please select a user to remove.");
|
showToast("Please select a user to remove.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!confirm("Are you sure you want to delete user " + usernameToRemove + "?")) {
|
if (!confirm("Are you sure you want to delete user " + usernameToRemove + "?")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fetch("removeUser.php", {
|
fetch("removeUser.php", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username: usernameToRemove })
|
body: JSON.stringify({ username: usernameToRemove })
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
showToast("User removed successfully!");
|
||||||
|
closeRemoveUserModal();
|
||||||
|
loadUserList();
|
||||||
|
} else {
|
||||||
|
showToast("Error: " + (data.error || "Could not remove user"));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.catch(error => console.error("Error removing user:", error));
|
||||||
.then(data => {
|
});
|
||||||
if (data.success) {
|
|
||||||
showToast("User removed successfully!");
|
|
||||||
closeRemoveUserModal();
|
|
||||||
loadUserList();
|
|
||||||
} else {
|
|
||||||
showToast("Error: " + (data.error || "Could not remove user"));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => console.error("Error removing user:", error));
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById("cancelRemoveUserBtn").addEventListener("click", function () {
|
document.getElementById("cancelRemoveUserBtn").addEventListener("click", function () {
|
||||||
closeRemoveUserModal();
|
closeRemoveUserModal();
|
||||||
});
|
});
|
||||||
|
|
||||||
export function checkAuthentication() {
|
export function checkAuthentication() {
|
||||||
sendRequest("checkAuth.php")
|
// Return the promise from sendRequest
|
||||||
|
return sendRequest("checkAuth.php")
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.setup) {
|
if (data.setup) {
|
||||||
window.setupMode = true;
|
window.setupMode = true;
|
||||||
@@ -128,7 +129,7 @@ export function checkAuthentication() {
|
|||||||
toggleVisibility("mainOperations", false);
|
toggleVisibility("mainOperations", false);
|
||||||
document.querySelector(".header-buttons").style.visibility = "hidden";
|
document.querySelector(".header-buttons").style.visibility = "hidden";
|
||||||
toggleVisibility("addUserModal", true);
|
toggleVisibility("addUserModal", true);
|
||||||
return;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
window.setupMode = false;
|
window.setupMode = false;
|
||||||
}
|
}
|
||||||
@@ -137,7 +138,7 @@ export function checkAuthentication() {
|
|||||||
toggleVisibility("mainOperations", true);
|
toggleVisibility("mainOperations", true);
|
||||||
toggleVisibility("uploadFileForm", true);
|
toggleVisibility("uploadFileForm", true);
|
||||||
toggleVisibility("fileListContainer", true);
|
toggleVisibility("fileListContainer", true);
|
||||||
// Check admin status to determine if Add/Remove User buttons should be shown.
|
// Show Add/Remove User buttons if admin.
|
||||||
if (data.isAdmin) {
|
if (data.isAdmin) {
|
||||||
const addUserBtn = document.getElementById("addUserBtn");
|
const addUserBtn = document.getElementById("addUserBtn");
|
||||||
const removeUserBtn = document.getElementById("removeUserBtn");
|
const removeUserBtn = document.getElementById("removeUserBtn");
|
||||||
@@ -150,6 +151,7 @@ export function checkAuthentication() {
|
|||||||
if (removeUserBtn) removeUserBtn.style.display = "none";
|
if (removeUserBtn) removeUserBtn.style.display = "none";
|
||||||
}
|
}
|
||||||
document.querySelector(".header-buttons").style.visibility = "visible";
|
document.querySelector(".header-buttons").style.visibility = "visible";
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
showToast("Please log in to continue.");
|
showToast("Please log in to continue.");
|
||||||
toggleVisibility("loginForm", true);
|
toggleVisibility("loginForm", true);
|
||||||
@@ -157,9 +159,13 @@ export function checkAuthentication() {
|
|||||||
toggleVisibility("uploadFileForm", false);
|
toggleVisibility("uploadFileForm", false);
|
||||||
toggleVisibility("fileListContainer", false);
|
toggleVisibility("fileListContainer", false);
|
||||||
document.querySelector(".header-buttons").style.visibility = "hidden";
|
document.querySelector(".header-buttons").style.visibility = "hidden";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => console.error("Error checking authentication:", error));
|
.catch(error => {
|
||||||
|
console.error("Error checking authentication:", error);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
window.checkAuthentication = checkAuthentication;
|
window.checkAuthentication = checkAuthentication;
|
||||||
|
|
||||||
@@ -202,4 +208,4 @@ function loadUserList() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => console.error("Error loading user list:", error));
|
.catch(error => console.error("Error loading user list:", error));
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- File List Section -->
|
<!-- File List Section -->
|
||||||
<div id="fileListContainer">
|
<div id="fileListContainer" style="display: none;">
|
||||||
<h2 id="fileListTitle">Files in (Root)</h2>
|
<h2 id="fileListTitle">Files in (Root)</h2>
|
||||||
<div id="fileListActions" class="file-list-actions">
|
<div id="fileListActions" class="file-list-actions">
|
||||||
<button id="deleteSelectedBtn" class="btn action-btn" style="display: none;">
|
<button id="deleteSelectedBtn" class="btn action-btn" style="display: none;">
|
||||||
|
|||||||
25
main.js
25
main.js
@@ -15,12 +15,9 @@ import {
|
|||||||
displayFilePreview,
|
displayFilePreview,
|
||||||
renameFile
|
renameFile
|
||||||
} from './fileManager.js';
|
} from './fileManager.js';
|
||||||
import {
|
import { loadFolderTree } from './folderManager.js';
|
||||||
loadFolderTree,
|
|
||||||
loadFolderList
|
|
||||||
} from './folderManager.js';
|
|
||||||
import { initUpload } from './upload.js';
|
import { initUpload } from './upload.js';
|
||||||
import { initAuth } from './auth.js';
|
import { initAuth, checkAuthentication } from './auth.js';
|
||||||
|
|
||||||
// Expose functions for inline handlers.
|
// Expose functions for inline handlers.
|
||||||
window.sendRequest = sendRequest;
|
window.sendRequest = sendRequest;
|
||||||
@@ -44,10 +41,16 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
showToast(message);
|
showToast(message);
|
||||||
sessionStorage.removeItem("welcomeMessage");
|
sessionStorage.removeItem("welcomeMessage");
|
||||||
}
|
}
|
||||||
window.currentFolder = "root";
|
checkAuthentication().then(authenticated => {
|
||||||
window.updateFileActionButtons = updateFileActionButtons;
|
if (authenticated) {
|
||||||
loadFileList(window.currentFolder);
|
window.currentFolder = "root";
|
||||||
initFileActions();
|
loadFileList(window.currentFolder);
|
||||||
initUpload();
|
initFileActions();
|
||||||
loadFolderTree();
|
initUpload();
|
||||||
|
loadFolderTree();
|
||||||
|
} else {
|
||||||
|
console.warn("User not authenticated. Data loading deferred.");
|
||||||
|
// Optionally redirect to login
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user