44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || empty($password)) {
|
|
header('Location: index.php?error=Email and password are required.');
|
|
exit();
|
|
}
|
|
|
|
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'];
|
|
|
|
if ($user['role'] === 'owner') {
|
|
header('Location: dashboard_owner.php');
|
|
} else {
|
|
header('Location: dashboard_client.php');
|
|
}
|
|
exit();
|
|
} else {
|
|
header('Location: index.php?error=Invalid email or password.');
|
|
exit();
|
|
}
|
|
} catch (PDOException $e) {
|
|
// In a real app, you would log this error.
|
|
header('Location: index.php?error=An internal error occurred.');
|
|
exit();
|
|
}
|
|
} else {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|