From 9baaeafd1c2f5dda82109ae97d2f453fcea0b60f Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 4 Oct 2026 08:11:52 +0200 Subject: [PATCH] feat: Implementiere Login- und Authentifizierungssystem Co-authored-by: aider (gemini/gemini-2.5-pro) --- public/index.php | 39 +++++++++++++-- src/Core/Auth.php | 91 +++++++++++++++++++++++++++++++++++ src/Models/User.php | 33 +++++++++++++ src/Views/login.php | 26 ++++++++++ src/Views/partials/header.php | 8 +++ 5 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 src/Core/Auth.php create mode 100644 src/Models/User.php create mode 100644 src/Views/login.php diff --git a/public/index.php b/public/index.php index 3e67e05..715d33a 100644 --- a/public/index.php +++ b/public/index.php @@ -3,17 +3,47 @@ // Lade den minimalistischen Autoloader require_once __DIR__ . '/../src/autoload.php'; +// Auth-System initialisieren +App\Core\Auth::start(); + use App\Core\Database; +use App\Core\Auth; use App\Models\Game; use App\Models\Tipp; +use App\Models\User; use App\Models\Round; use App\Models\Season; use App\Models\Ranking; $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); +$page = $_GET['page'] ?? null; -// Routing basierend auf Query-Parametern, um Probleme mit URL-Rewriting zu umgehen -if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { +// Home-Seite erkennen +if (($uri === '/' || $uri === '/index.php') && $page === null) { + $page = 'home'; +} + +// Routing basierend auf Query-Parametern +if ($page === 'login') { + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $email = $_POST['email'] ?? ''; + $password = $_POST['password'] ?? ''; + if (Auth::login($email, $password)) { + header('Location: /index.php?page=ranking'); + exit; + } else { + $error = 'Ungültige Anmeldedaten.'; + require __DIR__ . '/../src/Views/login.php'; + } + } else { + require __DIR__ . '/../src/Views/login.php'; + } +} elseif ($page === 'logout') { + Auth::logout(); + header('Location: /index.php?page=login'); + exit; +} elseif ($page === 'round' && isset($_GET['id'])) { + Auth::requireLogin(); try { $roundId = (int)$_GET['id']; $selectedRound = Round::getById($roundId); @@ -71,7 +101,8 @@ if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { http_response_code(500); echo "

Fehler: " . htmlspecialchars($e->getMessage()) . "

"; } -} elseif (($_GET['page'] ?? null) === 'ranking') { +} elseif ($page === 'ranking') { + Auth::requireLogin(); try { $currentSeason = Season::getCurrent(); if (!$currentSeason) { @@ -92,7 +123,7 @@ if (($_GET['page'] ?? null) === 'round' && isset($_GET['id'])) { http_response_code(500); echo "

Fehler: " . htmlspecialchars($e->getMessage()) . "

"; } -} elseif ($uri === '/' || $uri === '/index.php') { +} elseif ($page === 'home') { try { $db = Database::getConnection(); $userCount = $db->query("SELECT COUNT(*) FROM users")->fetchColumn(); diff --git a/src/Core/Auth.php b/src/Core/Auth.php new file mode 100644 index 0000000..786777a --- /dev/null +++ b/src/Core/Auth.php @@ -0,0 +1,91 @@ +prepare("SELECT * FROM users WHERE email = ?"); + $stmt->execute([$email]); + return $stmt->fetch(\PDO::FETCH_ASSOC); + } + + /** + * Findet einen Benutzer anhand seiner ID. + * + * @param int $id + * @return array|false + */ + public static function findById($id) { + $db = Database::getConnection(); + $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); + $stmt->execute([$id]); + return $stmt->fetch(\PDO::FETCH_ASSOC); + } +} diff --git a/src/Views/login.php b/src/Views/login.php new file mode 100644 index 0000000..9c3cf52 --- /dev/null +++ b/src/Views/login.php @@ -0,0 +1,26 @@ + + +

Login

+ + +

+ + +
+
+ + +
+
+ + +
+
+ +
+
+ + diff --git a/src/Views/partials/header.php b/src/Views/partials/header.php index d3541e0..77ae9f7 100644 --- a/src/Views/partials/header.php +++ b/src/Views/partials/header.php @@ -14,4 +14,12 @@ Tipps Rangliste Info +
+ + Hallo, ! + Logout + + Login + +