diff --git a/public/index.php b/public/index.php index 8a1454e..869aede 100644 --- a/public/index.php +++ b/public/index.php @@ -46,6 +46,57 @@ if ($page === 'login') { } else { require __DIR__ . '/../src/Views/login.php'; } +} elseif ($page === 'register') { + if (Auth::check()) { + header('Location: /index.php?page=ranking'); + exit; + } + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $data = [ + 'firstname' => $_POST['firstname'] ?? '', + 'lastname' => $_POST['lastname'] ?? '', + 'email' => $_POST['email'] ?? '', + 'password' => $_POST['password'] ?? '', + ]; + $password_confirm = $_POST['password_confirm'] ?? ''; + $error = null; + + if (empty($data['firstname']) || empty($data['lastname']) || empty($data['email']) || empty($data['password'])) { + $error = 'Alle Felder sind erforderlich.'; + } elseif ($data['password'] !== $password_confirm) { + $error = 'Die Passwörter stimmen nicht überein.'; + } elseif (User::findByEmail($data['email'])) { + $error = 'Diese E-Mail-Adresse ist bereits registriert.'; + } else { + try { + if (User::create($data)) { + // Automatically log in the new user + if (Auth::login($data['firstname'], $data['lastname'], $data['password'])) { + header('Location: /index.php?page=ranking'); + exit; + } else { + // This should not happen if creation was successful + $error = 'Anmeldung nach Registrierung fehlgeschlagen. Bitte versuchen Sie, sich manuell anzumelden.'; + require __DIR__ . '/../src/Views/login.php'; + exit; + } + } else { + $error = 'Ein Fehler ist bei der Registrierung aufgetreten.'; + } + } catch (\Throwable $e) { + // Could be a unique constraint violation from DB + $error = 'Ein Datenbankfehler ist aufgetreten. Die E-Mail könnte bereits vergeben sein.'; + } + } + + // Show form again with error + require __DIR__ . '/../src/Views/register.php'; + + } else { + // Show registration form + require __DIR__ . '/../src/Views/register.php'; + } } elseif ($page === 'logout') { Auth::logout(); header('Location: /index.php?page=login'); diff --git a/src/Views/login.php b/src/Views/login.php index 7329b58..aea8b21 100644 --- a/src/Views/login.php +++ b/src/Views/login.php @@ -38,6 +38,7 @@ require __DIR__ . '/partials/header.php';
+ Registrieren
diff --git a/src/Views/register.php b/src/Views/register.php new file mode 100644 index 0000000..3a8f529 --- /dev/null +++ b/src/Views/register.php @@ -0,0 +1,52 @@ + + +

Registrieren

+ + +

+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +