37970-vm/login.php
2026-01-30 14:32:19 +00:00

96 lines
3.7 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
session_start();
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
if ($email && $password) {
$db = db();
$stmt = $db->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['role'] = $user['role'];
$_SESSION['school_id'] = $user['school_id'];
$_SESSION['email'] = $user['email'];
// Redirect based on role
switch ($user['role']) {
case 'Super Admin':
header('Location: super-admin.php');
break;
case 'Admin':
header('Location: admin.php');
break;
case 'Teacher':
header('Location: teacher.php');
break;
case 'Parent':
header('Location: parent.php');
break;
default:
header('Location: index.php');
}
exit;
} else {
$error = "Invalid email or password.";
}
} else {
$error = "Please fill in all fields.";
}
}
$pageTitle = 'Login | SOMS Platform';
include 'includes/header.php';
?>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card shadow-lg border-0">
<div class="card-body p-5">
<div class="text-center mb-4">
<h2 class="fw-bold">Welcome Back</h2>
<p class="text-muted">Sign in to manage your school</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger small"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label class="form-label small fw-bold">Email Address</label>
<input type="email" name="email" class="form-control" placeholder="e.g., admin@school.edu.za" required>
</div>
<div class="mb-4">
<label class="form-label small fw-bold">Password</label>
<input type="password" name="password" class="form-control" placeholder="••••••••" required>
</div>
<button type="submit" class="btn btn-primary w-100 py-2 fw-bold">Sign In</button>
</form>
<div class="mt-4 text-center">
<p class="text-muted small">Don't have an account? <a href="register.php" class="text-primary fw-bold">Register School</a></p>
</div>
<div class="mt-4 p-3 bg-light rounded border">
<p class="mb-1 small fw-bold text-uppercase text-muted" style="letter-spacing: 1px;">Demo Credentials:</p>
<p class="mb-0 small"><strong>Admin:</strong> admin@sowetohigh.edu.za</p>
<p class="mb-0 small"><strong>Teacher:</strong> teacher@sowetohigh.edu.za</p>
<p class="mb-0 small text-muted">Password: <code>password123</code></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>