73 lines
1.8 KiB
PHP
73 lines
1.8 KiB
PHP
<?php
|
|
|
|
class LoginModel extends Model {
|
|
|
|
protected $email;
|
|
protected $passwort;
|
|
protected $errorArray = [];
|
|
|
|
// Getter und Setter für $email
|
|
public function getEmail() {
|
|
return $this->email;
|
|
}
|
|
|
|
public function setEmail($email) {
|
|
$this->email = trim($email); // trim entfernt Leerzeichen vorne und hinten
|
|
}
|
|
|
|
// Getter und Setter für $passwort
|
|
public function getPasswort() {
|
|
return $this->passwort;
|
|
}
|
|
|
|
public function setPasswort($passwort) {
|
|
$this->passwort = trim($passwort); // trim entfernt Leerzeichen vorne und hinten
|
|
}
|
|
|
|
|
|
public function getErrors() {
|
|
return $this->errorArray;
|
|
}
|
|
|
|
|
|
public function login() {
|
|
if (!$this->validate()) return false;
|
|
|
|
$db = Database::getInstance()->getConnection();
|
|
$stmt = $db->prepare("SELECT id, passwort, vorname, nachname FROM user WHERE email = ?");
|
|
$stmt->bind_param("s", $this->email);
|
|
$stmt->execute();
|
|
$stmt->store_result();
|
|
|
|
if ($stmt->num_rows !== 1) {
|
|
$this->errorArray['login'] = "E-Mail oder Passwort falsch.";
|
|
return false;
|
|
}
|
|
|
|
$stmt->bind_result($id, $dbHash, $vorname, $nachname);
|
|
$stmt->fetch();
|
|
|
|
if (!password_verify(trim($this->passwort), $dbHash)) {
|
|
$this->errorArray['login'] = "E-Mail oder Passwort falsch.";
|
|
return false;
|
|
}
|
|
|
|
|
|
$_SESSION['user_id'] = $id; // nur die User-ID
|
|
$_SESSION['vorname'] = $vorname; // optional für Anzeige
|
|
$_SESSION['nachname'] = $nachname; // optional für Anzeige
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
// --- Validierung ---
|
|
protected function validate() {
|
|
$errors = [];
|
|
if (empty($this->email)) $errors['email'] = "Bitte E-Mail eingeben.";
|
|
if (empty($this->passwort)) $errors['passwort'] = "Bitte Passwort eingeben.";
|
|
$this->errorArray = $errors;
|
|
return empty($errors);
|
|
}
|
|
}
|