Edit login.php via Editor

This commit is contained in:
Flatlogic Bot 2026-01-04 09:26:26 +00:00
parent 11ca3a78b6
commit fa1ebff600

View File

@ -1,9 +1,11 @@
<?php
session_start();
require_once 'session_config.php';
// If already logged in, redirect to dashboard
if (isset($_SESSION['role'])) {
header('Location: index.php');
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
$redirect = $_SESSION['redirect_after_login'] ?? 'index.php';
unset($_SESSION['redirect_after_login']);
header('Location: ' . $redirect);
exit();
}
@ -34,7 +36,7 @@ include 'partials/header.php';
<div class="d-grid">
<button class="btn btn-primary btn-lg" type="submit" id="submit-btn">
<span id="btn-text">Sign in</span>
<span id="btn-spinner" class="spinner-border spinner-border-sm" style="display: none;" role="status" aria-hidden="true"></span>
<span id="btn-spinner" class="spinner-border spinner-border-sm" style="display: none;"></span>
</button>
</div>
</form>
@ -60,10 +62,7 @@ document.getElementById('login-form').addEventListener('submit', async function(
const btnText = document.getElementById('btn-text');
const btnSpinner = document.getElementById('btn-spinner');
// Hide error message
errorDiv.style.display = 'none';
// Disable button and show spinner
submitBtn.disabled = true;
btnText.style.display = 'none';
btnSpinner.style.display = 'inline-block';
@ -74,20 +73,20 @@ document.getElementById('login-form').addEventListener('submit', async function(
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
body: JSON.stringify({ email, password }),
credentials: 'same-origin' // Important for sessions
});
const result = await response.json();
if (response.ok && result.success) {
// Successful login
window.location.href = 'index.php';
// Add a small delay to ensure session is written
setTimeout(() => {
window.location.href = result.redirect || 'index.php';
}, 100);
} else {
// Login failed
errorDiv.textContent = result.message || 'Invalid email or password.';
errorDiv.style.display = 'block';
// Re-enable button
submitBtn.disabled = false;
btnText.style.display = 'inline';
btnSpinner.style.display = 'none';
@ -96,8 +95,6 @@ document.getElementById('login-form').addEventListener('submit', async function(
console.error('Login error:', error);
errorDiv.textContent = 'Failed to connect to the server. Please try again.';
errorDiv.style.display = 'block';
// Re-enable button
submitBtn.disabled = false;
btnText.style.display = 'inline';
btnSpinner.style.display = 'none';