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