From db526e5bb2f793ae40681f12e3af6fa2493f8eef Mon Sep 17 00:00:00 2001 From: Max538 Date: Mon, 23 Jun 2025 14:22:43 +0200 Subject: [PATCH] implemented Controller logic --- Controller/AuthController.php | 130 ++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 13 deletions(-) diff --git a/Controller/AuthController.php b/Controller/AuthController.php index 646442c..011ad75 100644 --- a/Controller/AuthController.php +++ b/Controller/AuthController.php @@ -10,8 +10,12 @@ class AuthController private $db; private $validData = array(); private $errors = array(); - private $labels = array("name" => "Name", "email" => "E-Mail-Adresse", "content" => "Nachricht"); - + private $labels = array( + "email" => "E-Mail-Adresse", + "password" => "Passwort", + "password_repeat" => "Passwort wiederholen", + "old_password" => "Altes Passwort" + ); public function __construct($view) { @@ -28,28 +32,128 @@ class AuthController ]); } - public function showConfirmation() + public function showConfirmation($message = "Aktion erfolgreich.") { - + $this->view->setVars(['message' => $message]); + $this->view->render('auth/confirmation'); } - public function validateForm(){ + public function validateForm() + { foreach ($this->labels as $index => $value) { if (!isset($_POST[$index]) || empty($_POST[$index])) { $this->errors[$index] = "Bitte " . $value . " angeben"; } else { - $this->validData[$index] = $_POST[$index]; + $this->validData[$index] = trim($_POST[$index]); } } - if (count($this->errors) > 0) { - $this->view->setDoMethodName("showContactForm"); - $this->showContactForm(); + if (!empty($this->errors)) { + $this->view->setDoMethodName("showAuthForm"); + $this->showAuthForm(); } else { - if ($this->db->writeContactData($this->validData)) { - $this->view->setDoMethodName("showConfirmation"); - $this->showConfirmation(); - } + $this->view->setDoMethodName("showConfirmation"); + $this->showConfirmation(); + } + } + + public function login() + { + $email = $_POST['email'] ?? ''; + $password = $_POST['password'] ?? ''; + + if (empty($email) || empty($password)) { + $this->errors['login'] = "Bitte E-Mail und Passwort eingeben."; + return $this->showAuthForm(); + } + + $result = $this->db->login($email, $password); + + if ($result === true) { + $_SESSION['user'] = $email; + $this->showConfirmation("Login erfolgreich."); + } elseif (is_string($result)) { + $this->errors['login'] = $result; + $this->showAuthForm(); + } else { + $this->errors['login'] = "Login fehlgeschlagen."; + $this->showAuthForm(); + } + } + + public function register() + { + $data = [ + 'email' => $_POST['email'] ?? '', + 'password' => $_POST['password'] ?? '', + 'password_repeat' => $_POST['password_repeat'] ?? '', + 'straße' => $_POST['straße'] ?? '', + 'hausnr' => $_POST['hausnr'] ?? '', + 'ort' => $_POST['ort'] ?? '', + 'postleitzahl' => $_POST['postleitzahl'] ?? '', + 'land' => $_POST['land'] ?? '', + 'vorname' => $_POST['vorname'] ?? '', + 'nachname' => $_POST['nachname'] ?? '', + 'tel' => $_POST['tel'] ?? '' + ]; + + if (!$this->db->checkDoublePw($data['password'], $data['password_repeat'])) { + $this->errors['password'] = "Passwörter stimmen nicht überein."; + return $this->showAuthForm(); + } + + $result = $this->db->register( + $data['email'], $data['password'], $data['straße'], $data['hausnr'], + $data['ort'], $data['postleitzahl'], $data['land'], + $data['vorname'], $data['nachname'], $data['tel'] + ); + + if ($result === true) { + $this->showConfirmation("Registrierung erfolgreich."); + } elseif (is_string($result)) { + $this->errors['register'] = $result; + $this->showAuthForm(); + } else { + $this->errors['register'] = "Registrierung fehlgeschlagen."; + $this->showAuthForm(); + } + } + + public function forgotPassword() + { + $email = $_POST['email'] ?? ''; + + if (empty($email)) { + $this->errors['email'] = "Bitte E-Mail-Adresse angeben."; + return $this->showAuthForm(); + } + + $this->db->pwForgot($email); + $this->showConfirmation("Ein temporäres Passwort wurde an Ihre E-Mail gesendet."); + } + + public function changePassword() + { + $email = $_POST['email'] ?? ''; + $oldpw = $_POST['old_password'] ?? ''; + $newpw = $_POST['password'] ?? ''; + $repeat = $_POST['password_repeat'] ?? ''; + + if (!$this->db->checkDoublePw($newpw, $repeat)) { + $this->errors['password'] = "Neue Passwörter stimmen nicht überein."; + return $this->showAuthForm(); + } + + $result = $this->db->updatePassword($email, $oldpw, $newpw); + + if ($result === true) { + $this->showConfirmation("Passwort erfolgreich geändert."); + } elseif (is_string($result)) { + $this->errors['password'] = $result; + $this->showAuthForm(); + } else { + $this->errors['password'] = "Fehler beim Aktualisieren des Passworts."; + $this->showAuthForm(); } } }