89 lines
3.4 KiB
PHP
89 lines
3.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$email = $password = '';
|
|
$errors = [];
|
|
|
|
// If user is already logged in, redirect to admin dashboard
|
|
if (isset($_SESSION['user_id'])) {
|
|
header("Location: admin/index.php");
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($email)) {
|
|
$errors['email'] = 'Email is required';
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$errors['password'] = 'Password is required';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_name'] = $user['name'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
|
|
header("Location: admin/index.php");
|
|
exit;
|
|
} else {
|
|
$errors['login'] = 'Invalid email or password';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors['db'] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
$pageTitle = "Login";
|
|
include 'partials/header.php';
|
|
?>
|
|
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card mt-5">
|
|
<div class="card-body">
|
|
<h3 class="card-title text-center">Admin Login</h3>
|
|
<?php if (!empty($errors['login'])): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($errors['login']); ?></div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($errors['db'])): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($errors['db']); ?></div>
|
|
<?php endif; ?>
|
|
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email address</label>
|
|
<input type="email" class="form-control <?php echo !empty($errors['email']) ? 'is-invalid' : ''; ?>" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>">
|
|
<?php if (!empty($errors['email'])): ?>
|
|
<div class="invalid-feedback"><?php echo $errors['email']; ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control <?php echo !empty($errors['password']) ? 'is-invalid' : ''; ?>" id="password" name="password">
|
|
<?php if (!empty($errors['password'])): ?>
|
|
<div class="invalid-feedback"><?php echo $errors['password']; ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Login</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|