62 lines
2.1 KiB
PHP
62 lines
2.1 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'includes/header.php';
|
|
|
|
$error = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$pdo = db();
|
|
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || empty($password)) {
|
|
$error = t('fill_all_fields');
|
|
} else {
|
|
try {
|
|
$stmt = $pdo->prepare('SELECT id, password, role FROM users WHERE email = ?');
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_email'] = $email;
|
|
$_SESSION['user_role'] = $user['role'];
|
|
header('Location: index.php');
|
|
exit();
|
|
} else {
|
|
$error = t('invalid_credentials');
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<main class="container">
|
|
<section class="auth-form">
|
|
<h1><?= t('login_heading') ?></h1>
|
|
<p><?= t('login_subtitle') ?></p>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="error-message"><?= $error ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login.php" method="POST" id="login-form" novalidate>
|
|
<div class="form-group">
|
|
<label for="email"><?= t('email') ?></label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password"><?= t('password') ?></label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary"><?= t('login_button') ?></button>
|
|
</div>
|
|
</form>
|
|
<p class="auth-switch"><?= t('no_account') ?> <a href="register.php"><?= t('register_link') ?></a></p>
|
|
</section>
|
|
</main>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|