95 lines
3.4 KiB
PHP
95 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
@ini_set('display_errors', '1');
|
|
@error_reporting(E_ALL);
|
|
@date_default_timezone_set('UTC');
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
session_start();
|
|
|
|
// Redirect if already logged in
|
|
if (isset($_SESSION['user_role'])) {
|
|
header('Location: ' . $_SESSION['user_role'] . '_dashboard.php');
|
|
exit;
|
|
}
|
|
|
|
$error_message = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
$role = $_POST['role'] ?? '';
|
|
|
|
if (empty($email) || empty($password) || empty($role)) {
|
|
$error_message = 'Please provide email, password, and select a role.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND role = ?");
|
|
$stmt->execute([$email, $role]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
// Password is correct, set session variables
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
$_SESSION['user_email'] = $user['email'];
|
|
$_SESSION['user_full_name'] = $user['full_name'];
|
|
|
|
// Redirect to the respective dashboard
|
|
header('Location: ' . $user['role'] . '_dashboard.php');
|
|
exit;
|
|
} else {
|
|
$error_message = 'Invalid email, password, or role.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
?>
|
|
<body class="text-center">
|
|
<main class="form-signin">
|
|
<form method="POST">
|
|
<h1 class="h3 mb-3 fw-normal">School Leave System</h1>
|
|
<p class="mb-4">Please sign in to continue</p>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo htmlspecialchars($error_message); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="form-floating mb-3">
|
|
<input type="email" class="form-control" id="floatingInput" name="email" placeholder="name@example.com" required>
|
|
<label for="floatingInput">Email address</label>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="password" class="form-control" id="floatingPassword" name="password" placeholder="Password" required>
|
|
<label for="floatingPassword">Password</label>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<select class="form-select" id="floatingRole" name="role" required>
|
|
<option value="" selected disabled>Select your role</option>
|
|
<option value="student">Siswa (Student)</option>
|
|
<option value="teacher">Wali Kelas (Homeroom Teacher)</option>
|
|
<option value="admin">Admin</option>
|
|
</select>
|
|
<label for="floatingRole">Role</label>
|
|
</div>
|
|
|
|
<div class="checkbox mb-3">
|
|
<label>
|
|
<input type="checkbox" value="remember-me"> Remember me
|
|
</label>
|
|
</div>
|
|
<button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button>
|
|
<p class="mt-3">
|
|
<a href="forgot_password.php">Forgot password?</a>
|
|
</p>
|
|
<p class="mt-5 mb-3 text-muted">© <?php echo date("Y"); ?>. All rights reserved.</p>
|
|
</form>
|
|
</main>
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|