feat: Füge Benutzerregistrierung hinzu und verlinke im Login
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
parent
a357d04b06
commit
d0984398d1
3 changed files with 104 additions and 0 deletions
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue