120 lines
5.1 KiB
PHP
120 lines
5.1 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
// Redirect to the appropriate dashboard if already logged in
|
|
if (isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
|
|
header("Location: admin/index.php");
|
|
} else {
|
|
header("Location: dashboard.php");
|
|
}
|
|
exit();
|
|
}
|
|
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($username)) {
|
|
$errors[] = 'Username is required.';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
// Allow login by username only per new schema
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1");
|
|
$stmt->execute(['username' => $username]);
|
|
$user = $stmt->fetch();
|
|
|
|
// Note: The 'password' column stores the hash
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
if (isset($user['status']) && $user['status'] !== 'active') {
|
|
// Kept specific status check logic if status column existed, but since schema is simple, this block is mostly for safety if schema evolves.
|
|
// Current schema doesn't have status, but if it did, we'd check it.
|
|
// The setup_project.php removed the status column from users table to fit the simple requirements.
|
|
// So we proceed.
|
|
}
|
|
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role'];
|
|
|
|
// Redirect to the appropriate dashboard
|
|
if ($user['role'] === 'admin') {
|
|
header("Location: admin/index.php");
|
|
} else {
|
|
header("Location: dashboard.php");
|
|
}
|
|
exit();
|
|
|
|
} else {
|
|
$errors[] = 'Invalid login credentials.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
$errors[] = "An internal error occurred. Please try again later.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login - Car Sells in Afghanistan</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<div class="form-container">
|
|
<div class="card auth-card shadow-lg border-0">
|
|
<div class="card-body p-5">
|
|
<div class="text-center mb-5">
|
|
<h1 class="h2 fw-bold">Welcome Back!</h1>
|
|
<p class="text-muted">Login to access your account.</p>
|
|
</div>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p class="mb-0 small"><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login.php" method="POST">
|
|
<div class="mb-4">
|
|
<label for="username" class="form-label fw-semibold">Username or Email</label>
|
|
<input type="text" id="username" name="username" class="form-control form-control-lg" placeholder="admin@gmail.com" required value="<?php echo isset($username) ? htmlspecialchars($username) : ''; ?>">
|
|
</div>
|
|
<div class="mb-4">
|
|
<label for="password" class="form-label fw-semibold">Password</label>
|
|
<input type="password" id="password" name="password" class="form-control form-control-lg" placeholder="••••••••" required>
|
|
</div>
|
|
<div class="d-grid mt-5">
|
|
<button type="submit" class="btn btn-primary btn-lg shadow-sm">Login</button>
|
|
</div>
|
|
</form>
|
|
|
|
<p class="text-center text-muted mt-4">
|
|
Don't have an account? <a href="register.php" class="text-primary text-decoration-none fw-semibold">Create one now</a>.
|
|
</p>
|
|
<p class="text-center text-muted mt-2">
|
|
<a href="index.php" class="text-muted text-decoration-none small"><i class="bi bi-arrow-left"></i> Back to Home</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|