105 lines
4.0 KiB
PHP
105 lines
4.0 KiB
PHP
<?php
|
|
require_once 'session_config.php';
|
|
|
|
// If already logged in, redirect to dashboard
|
|
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();
|
|
}
|
|
|
|
include 'partials/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid login-container">
|
|
<div class="card shadow-lg login-card p-4">
|
|
<div class="card-body">
|
|
<div class="text-center mb-4">
|
|
<i class="bi-building fs-1" style="color: var(--primary-color);"></i>
|
|
<h1 class="h3 mb-3 fw-bold">Providencia ERP</h1>
|
|
<p class="text-muted">Please sign in to continue</p>
|
|
</div>
|
|
|
|
<div id="error-message" class="alert alert-danger" style="display: none;"></div>
|
|
|
|
<form id="login-form">
|
|
<div class="form-floating mb-3">
|
|
<input type="email" class="form-control" id="email" name="email" placeholder="name@example.com" required>
|
|
<label for="email">Email address</label>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
|
|
<label for="password">Password</label>
|
|
</div>
|
|
|
|
<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;"></span>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
<div class="mt-3 text-center">
|
|
<small class="text-muted">Hint: Try one of these emails with the password 'password':<br>
|
|
accountant@example.com<br>
|
|
secretary@example.com<br>
|
|
headteacher@example.com
|
|
</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById('login-form').addEventListener('submit', async function(e) {
|
|
e.preventDefault();
|
|
|
|
const email = document.getElementById('email').value;
|
|
const password = document.getElementById('password').value;
|
|
const errorDiv = document.getElementById('error-message');
|
|
const submitBtn = document.getElementById('submit-btn');
|
|
const btnText = document.getElementById('btn-text');
|
|
const btnSpinner = document.getElementById('btn-spinner');
|
|
|
|
errorDiv.style.display = 'none';
|
|
submitBtn.disabled = true;
|
|
btnText.style.display = 'none';
|
|
btnSpinner.style.display = 'inline-block';
|
|
|
|
try {
|
|
const response = await fetch('api/login.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ email, password }),
|
|
credentials: 'same-origin' // Important for sessions
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (response.ok && result.success) {
|
|
// Add a small delay to ensure session is written
|
|
setTimeout(() => {
|
|
window.location.href = result.redirect || 'index.php';
|
|
}, 100);
|
|
} else {
|
|
errorDiv.textContent = result.message || 'Invalid email or password.';
|
|
errorDiv.style.display = 'block';
|
|
submitBtn.disabled = false;
|
|
btnText.style.display = 'inline';
|
|
btnSpinner.style.display = 'none';
|
|
}
|
|
} catch (error) {
|
|
console.error('Login error:', error);
|
|
errorDiv.textContent = 'Failed to connect to the server. Please try again.';
|
|
errorDiv.style.display = 'block';
|
|
submitBtn.disabled = false;
|
|
btnText.style.display = 'inline';
|
|
btnSpinner.style.display = 'none';
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|