auth and session changes
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$usersFile = USERS_DIR . USERS_FILE;
|
||||
|
||||
62
auth.js
62
auth.js
@@ -6,13 +6,10 @@ import { toggleVisibility } from './domUtils.js';
|
||||
import { loadFileList } from './fileManager.js';
|
||||
|
||||
export function initAuth() {
|
||||
// On initial load, show the login form and hide the main operations.
|
||||
toggleVisibility("loginForm", true);
|
||||
toggleVisibility("mainOperations", false);
|
||||
// Ensure header buttons are hidden.
|
||||
document.querySelector(".header-buttons").style.visibility = "hidden";
|
||||
// First, check if the user is already authenticated.
|
||||
checkAuthentication();
|
||||
|
||||
// Set up the authentication form listener.
|
||||
// Attach event listener for login.
|
||||
document.getElementById("authForm").addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
const formData = {
|
||||
@@ -24,38 +21,35 @@ export function initAuth() {
|
||||
.then(data => {
|
||||
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.
|
||||
|
||||
1
auth.php
1
auth.php
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$usersFile = USERS_DIR . USERS_FILE;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Check if users.txt is empty or doesn't exist
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
session_start();
|
||||
// config.php
|
||||
define('UPLOAD_DIR', '/var/www/uploads/');
|
||||
define('BASE_URL', 'http://yourwebsite/uploads/');
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Check authentication.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Ensure user is authenticated
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header("Cache-Control: no-cache, no-store, must-revalidate");
|
||||
header("Pragma: no-cache");
|
||||
header("Expires: 0");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true ||
|
||||
!isset($_SESSION['isAdmin']) || $_SESSION['isAdmin'] !== true) {
|
||||
|
||||
4
main.js
4
main.js
@@ -36,6 +36,8 @@ window.currentFolder = "root";
|
||||
|
||||
// DOMContentLoaded initialization.
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
// Initialize authentication and user management.
|
||||
initAuth();
|
||||
window.currentFolder = window.currentFolder || "root";
|
||||
loadFileList(window.currentFolder);
|
||||
loadCopyMoveFolderList();
|
||||
@@ -43,6 +45,4 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||
initUpload();
|
||||
loadFolderList();
|
||||
updateFileActionButtons();
|
||||
// Initialize authentication and user management.
|
||||
initAuth();
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Check authentication.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$usersFile = USERS_DIR . USERS_FILE;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
header("Cache-Control: no-cache, no-store, must-revalidate");
|
||||
header("Pragma: no-cache");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Ensure user is authenticated
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||
|
||||
Reference in New Issue
Block a user