38 lines
1008 B
PHP
38 lines
1008 B
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: /login.php?error=invalid_input');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare("SELECT id, password, role FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
// Password is correct, start session
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
|
|
header('Location: /dashboard.php');
|
|
exit;
|
|
} else {
|
|
header('Location: /login.php?error=auth_failed');
|
|
exit;
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
header('Location: /login.php?error=db_error');
|
|
exit;
|
|
}
|
|
}
|