churchtube/login.php

64 lines
2.1 KiB
PHP

<?php
require_once 'includes/db.php';
require_once 'includes/auth.php';
if (isLoggedIn()) {
header('Location: index.php');
exit;
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['user_role'] = $user['role'];
logEvent('auth', "User logged in: $username");
header('Location: index.php');
exit;
} else {
logEvent('auth', "FAILED login attempt for username: $username");
$error = "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login | ChurchTube</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="centered-container">
<a href="index.php" class="logo" style="display: block; text-align: center; margin-bottom: 24px;">ChurchTube</a>
<h2>Login</h2>
<?php if ($error): ?>
<div style="color: #ff4081; margin-bottom: 16px;"><?= $error ?></div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label class="form-label">Username</label>
<input type="text" name="username" class="form-control" required autofocus>
</div>
<div class="form-group">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Login</button>
</form>
<p style="margin-top: 20px; text-align: center; color: var(--text-muted);">
Don't have an account? <a href="register.php" style="color: var(--primary-color);">Register</a>
</p>
</div>
</body>
</html>