73 lines
3.0 KiB
PHP
73 lines
3.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = 'Please enter both username and password.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = ?');
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role'];
|
|
header('Location: admin.php');
|
|
exit;
|
|
} else {
|
|
$error = 'Invalid username or password.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
// In a real app, you'd log this error.
|
|
$error = 'An error occurred. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
|
|
$page_title = 'Admin Login';
|
|
require_once __DIR__ . '/header.php';
|
|
?>
|
|
|
|
<div class="flex items-center justify-center min-h-full">
|
|
<div class="w-full max-w-md p-8 space-y-6 bg-white rounded-lg shadow-md">
|
|
<h1 class="text-2xl font-bold text-center text-gray-900">Admin Login</h1>
|
|
<?php if ($error): ?>
|
|
<div class="p-4 text-sm text-red-700 bg-red-100 rounded-lg" role="alert">
|
|
<?php echo htmlspecialchars($error); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form action="login.php" method="post" class="space-y-6">
|
|
<div>
|
|
<label for="username" class="text-sm font-medium text-gray-700">Username</label>
|
|
<div class="mt-1">
|
|
<input type="text" id="username" name="username" required
|
|
class="w-full px-3 py-2 placeholder-gray-400 border border-gray-300 rounded-md shadow-sm appearance-none focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label for="password" class="text-sm font-medium text-gray-700">Password</label>
|
|
<div class="mt-1">
|
|
<input type="password" id="password" name="password" required
|
|
class="w-full px-3 py-2 placeholder-gray-400 border border-gray-300 rounded-md shadow-sm appearance-none focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<button type="submit"
|
|
class="w-full px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent rounded-md shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
|
|
Sign in
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/footer.php'; ?>
|