feat: Implementiere Login- und Authentifizierungssystem
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
parent
eed4c00645
commit
9baaeafd1c
5 changed files with 193 additions and 4 deletions
|
|
@ -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 "<p style='color:red;'>Fehler: " . htmlspecialchars($e->getMessage()) . "</p>";
|
||||
}
|
||||
} 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 "<p style='color:red;'>Fehler: " . htmlspecialchars($e->getMessage()) . "</p>";
|
||||
}
|
||||
} elseif ($uri === '/' || $uri === '/index.php') {
|
||||
} elseif ($page === 'home') {
|
||||
try {
|
||||
$db = Database::getConnection();
|
||||
$userCount = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
|
||||
|
|
|
|||
91
src/Core/Auth.php
Normal file
91
src/Core/Auth.php
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Auth {
|
||||
private static $user = null;
|
||||
|
||||
/**
|
||||
* Startet die Session, falls noch nicht geschehen.
|
||||
*/
|
||||
public static function start() {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Versucht, den Benutzer anzumelden.
|
||||
*
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
public static function login($email, $password) {
|
||||
$user = User::findByEmail($email);
|
||||
|
||||
if ($user && password_verify($password, $user['password'])) {
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Meldet den Benutzer ab.
|
||||
*/
|
||||
public static function logout() {
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
unset($_SESSION['user_id']);
|
||||
}
|
||||
session_destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob ein Benutzer angemeldet ist.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function check() {
|
||||
return isset($_SESSION['user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die ID des aktuell angemeldeten Benutzers zurück.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public static function id() {
|
||||
return $_SESSION['user_id'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den aktuell angemeldeten Benutzer zurück.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function user() {
|
||||
if (!self::check()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (self::$user === null) {
|
||||
self::$user = User::findById(self::id());
|
||||
}
|
||||
|
||||
return self::$user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Leitet zur Login-Seite weiter, wenn der Benutzer nicht angemeldet ist.
|
||||
*/
|
||||
public static function requireLogin() {
|
||||
if (!self::check()) {
|
||||
header('Location: /index.php?page=login');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/Models/User.php
Normal file
33
src/Models/User.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
class User {
|
||||
/**
|
||||
* Findet einen Benutzer anhand seiner E-Mail-Adresse.
|
||||
*
|
||||
* @param string $email
|
||||
* @return array|false
|
||||
*/
|
||||
public static function findByEmail($email) {
|
||||
$db = Database::getConnection();
|
||||
$stmt = $db->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);
|
||||
}
|
||||
}
|
||||
26
src/Views/login.php
Normal file
26
src/Views/login.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
$pageTitle = 'Login';
|
||||
require __DIR__ . '/partials/header.php';
|
||||
?>
|
||||
|
||||
<h1>Login</h1>
|
||||
|
||||
<?php if (isset($error)): ?>
|
||||
<p style="color: red;"><?php echo htmlspecialchars($error); ?></p>
|
||||
<?php endif; ?>
|
||||
|
||||
<form action="/index.php?page=login" method="post">
|
||||
<div>
|
||||
<label for="email">E-Mail:</label>
|
||||
<input type="email" id="email" name="email" required>
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">Passwort:</label>
|
||||
<input type="password" id="password" name="password" required>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit">Anmelden</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php require __DIR__ . '/partials/footer.php'; ?>
|
||||
|
|
@ -14,4 +14,12 @@
|
|||
<a href="#">Tipps</a>
|
||||
<a href="/index.php?page=ranking">Rangliste</a>
|
||||
<a href="#">Info</a>
|
||||
<div style="margin-left: auto;">
|
||||
<?php if (App\Core\Auth::check()): ?>
|
||||
<span style="margin-right: 1em;">Hallo, <?php echo htmlspecialchars(App\Core\Auth::user()['name']); ?>!</span>
|
||||
<a href="/index.php?page=logout">Logout</a>
|
||||
<?php else: ?>
|
||||
<a href="/index.php?page=login">Login</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue