From 05f53bf8ca0765e2a09fd7c3bf30a0c46b6bdf46 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 7 Mar 2025 05:24:45 -0500 Subject: [PATCH] auth and session changes --- addUser.php | 1 - auth.js | 62 +++++++++++++++++++++---------------------------- auth.php | 1 - checkAuth.php | 1 - config.php | 1 + copyFiles.php | 1 - deleteFiles.php | 1 - getFileList.php | 1 - getUsers.php | 1 - main.js | 4 ++-- moveFiles.php | 1 - removeUser.php | 1 - renameFile.php | 1 - saveFile.php | 1 - upload.php | 1 - 15 files changed, 30 insertions(+), 49 deletions(-) diff --git a/addUser.php b/addUser.php index 6dce62b..55fa83f 100644 --- a/addUser.php +++ b/addUser.php @@ -1,6 +1,5 @@ { console.log("Login response:", data); if (data.success) { - console.log("Login successful."); - // On successful login, hide the login form and show main operations. - toggleVisibility("loginForm", false); - toggleVisibility("mainOperations", true); - toggleVisibility("uploadFileForm", true); - toggleVisibility("fileListContainer", true); - // Check if the user is an admin. - if (data.isAdmin) { - // Show Add and Remove User buttons for admin. - const addUserBtn = document.getElementById("addUserBtn"); - const removeUserBtn = document.getElementById("removeUserBtn"); - if (addUserBtn) addUserBtn.style.display = "block"; - if (removeUserBtn) removeUserBtn.style.display = "block"; - } else { - // Hide Add and Remove User buttons for non-admin. - const addUserBtn = document.getElementById("addUserBtn"); - const removeUserBtn = document.getElementById("removeUserBtn"); - if (addUserBtn) addUserBtn.style.display = "none"; - if (removeUserBtn) removeUserBtn.style.display = "none"; - } - // Show header buttons (at least the Logout button) always. - document.querySelector(".header-buttons").style.visibility = "visible"; - // Refresh the file list immediately using the current folder. - loadFileList(window.currentFolder || "root"); - // Optionally, you can also call checkAuthentication() to update UI further. - checkAuthentication(); + console.log("✅ Login successful."); + updateUIOnLogin(data.isAdmin); + checkAuthentication(); // Double-check session persistence. } else { alert("Login failed: " + (data.error || "Unknown error")); } }) - .catch(error => console.error("Error logging in:", error)); + .catch(error => console.error("❌ Error logging in:", error)); }); +} + +// Helper function to update UI based on authentication. +function updateUIOnLogin(isAdmin) { + toggleVisibility("loginForm", false); + toggleVisibility("mainOperations", true); + toggleVisibility("uploadFileForm", true); + toggleVisibility("fileListContainer", true); + + if (isAdmin) { + document.getElementById("addUserBtn").style.display = "block"; + document.getElementById("removeUserBtn").style.display = "block"; + } else { + document.getElementById("addUserBtn").style.display = "none"; + document.getElementById("removeUserBtn").style.display = "none"; + } + + document.querySelector(".header-buttons").style.visibility = "visible"; + loadFileList(window.currentFolder || "root"); +} // Set up the logout button. document.getElementById("logoutBtn").addEventListener("click", function () { @@ -141,12 +135,10 @@ export function initAuth() { document.getElementById("cancelRemoveUserBtn").addEventListener("click", function () { closeRemoveUserModal(); }); -} export function checkAuthentication() { sendRequest("checkAuth.php") .then(data => { - console.log("Authentication check:", data); if (data.setup) { window.setupMode = true; // In setup mode, hide login and main operations; show Add User modal. diff --git a/auth.php b/auth.php index 613c3ff..5970bda 100644 --- a/auth.php +++ b/auth.php @@ -1,6 +1,5 @@