116 lines
4.7 KiB
PHP
116 lines
4.7 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 = [];
|
|
$login_input = ''; // Store input to repopulate form
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$login_input = trim($_POST['username'] ?? ''); // This field now accepts user OR email
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($login_input)) {
|
|
$errors[] = 'Username or Email is required.';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
// Allow login by username OR email
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :input OR email = :input LIMIT 1");
|
|
$stmt->execute(['input' => $login_input]);
|
|
$user = $stmt->fetch();
|
|
|
|
// Note: The 'password' column stores the hash
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
// Login Success
|
|
$_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 or admin@gmail.com" required value="<?php echo htmlspecialchars($login_input); ?>">
|
|
</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>
|