93 lines
3.6 KiB
PHP
93 lines
3.6 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header("Location: index.php");
|
|
exit();
|
|
}
|
|
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($email)) {
|
|
$errors[] = 'Email is required';
|
|
}
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
|
|
$stmt->execute(['email' => $email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password_hash'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role'];
|
|
header("Location: index.php");
|
|
exit();
|
|
} else {
|
|
$errors[] = 'Invalid email or password';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!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://rsms.me/inter/inter.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body class="bg-slate-50 dark:bg-slate-900 text-slate-800 dark:text-slate-200">
|
|
|
|
<?php include 'partials/navbar.php'; ?>
|
|
|
|
<main class="container mx-auto px-4 py-8">
|
|
<div class="max-w-md mx-auto bg-white dark:bg-slate-800 rounded-lg shadow-md p-8">
|
|
<h1 class="text-2xl font-bold text-center mb-6">Login to Your Account</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="bg-red-100 dark:bg-red-900 border border-red-400 text-red-700 dark:text-red-200 px-4 py-3 rounded relative mb-4" role="alert">
|
|
<ul>
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?php echo htmlspecialchars($error); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login.php" method="POST">
|
|
<div class="mb-4">
|
|
<label for="email" class="block text-sm font-medium mb-2">Email Address</label>
|
|
<input type="email" id="email" name="email" class="w-full px-3 py-2 bg-slate-100 dark:bg-slate-700 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500" required>
|
|
</div>
|
|
<div class="mb-6">
|
|
<label for="password" class="block text-sm font-medium mb-2">Password</label>
|
|
<input type="password" id="password" name="password" class="w-full px-3 py-2 bg-slate-100 dark:bg-slate-700 rounded-md focus:outline-none focus:ring-2 focus:ring-orange-500" required>
|
|
</div>
|
|
<button type="submit" class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-4 rounded-md transition duration-300">Login</button>
|
|
</form>
|
|
<p class="text-center text-sm mt-6">
|
|
Don't have an account? <a href="register.php" class="text-orange-500 hover:underline">Register here</a>.
|
|
</p>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'partials/footer.php'; ?>
|
|
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|