Embed the password-change form directly on the profile page

Mein Profil previously just linked out to change_password.php. Now
it has its own inline form (same validation as change_password.php),
so changing your password no longer requires leaving the page.
change_password.php still handles the forced first-login flow.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-09-18 17:57:04 +02:00
parent 706cf97d64
commit 3f27a23ae0

View file

@ -3,20 +3,38 @@ require_once __DIR__ . '/includes/bootstrap.php';
require_login();
$user = current_user();
$error = null;
$profileError = null;
$passwordError = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
$action = (string) ($_POST['action'] ?? '');
$displayName = trim((string) ($_POST['display_name'] ?? ''));
if ($displayName === '') {
$error = 'Anzeigename darf nicht leer sein.';
} else {
db()->prepare('UPDATE users SET display_name = ? WHERE id = ?')->execute([$displayName, $user['id']]);
set_flash('success', 'Profil wurde aktualisiert.');
redirect('/profile.php');
if ($action === 'update_profile') {
$displayName = trim((string) ($_POST['display_name'] ?? ''));
if ($displayName === '') {
$profileError = 'Anzeigename darf nicht leer sein.';
} else {
db()->prepare('UPDATE users SET display_name = ? WHERE id = ?')->execute([$displayName, $user['id']]);
set_flash('success', 'Profil wurde aktualisiert.');
redirect('/profile.php');
}
$user['display_name'] = $displayName;
} elseif ($action === 'change_password') {
$newPassword = (string) ($_POST['new_password'] ?? '');
$newPasswordConfirm = (string) ($_POST['new_password_confirm'] ?? '');
if (strlen($newPassword) < 8) {
$passwordError = 'Das neue Passwort muss mindestens 8 Zeichen lang sein.';
} elseif ($newPassword !== $newPasswordConfirm) {
$passwordError = 'Die Passwörter stimmen nicht überein.';
} else {
db()->prepare('UPDATE users SET password_hash = ?, must_change_password = 0 WHERE id = ?')
->execute([password_hash($newPassword, PASSWORD_DEFAULT), $user['id']]);
set_flash('success', 'Passwort wurde geändert.');
redirect('/profile.php');
}
}
$user['display_name'] = $displayName;
}
$pageTitle = 'Mein Profil';
@ -29,11 +47,12 @@ include __DIR__ . '/includes/header.php';
<div class="panel">
<h2>Anzeigename</h2>
<?php if ($error): ?>
<div class="flash flash-error"><?= e($error) ?></div>
<?php if ($profileError): ?>
<div class="flash flash-error"><?= e($profileError) ?></div>
<?php endif; ?>
<form method="post" action="/profile.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="update_profile">
<div>
<label for="display_name">Anzeigename</label>
<input type="text" id="display_name" name="display_name" value="<?= e($user['display_name']) ?>" required>
@ -43,9 +62,24 @@ include __DIR__ . '/includes/header.php';
</div>
<div class="panel">
<h2>Passwort</h2>
<p class="text-muted">Du kannst dein Passwort jederzeit ändern.</p>
<a class="button button-secondary" href="/change_password.php">Passwort ändern</a>
<h2>Passwort ändern</h2>
<?php if ($passwordError): ?>
<div class="flash flash-error"><?= e($passwordError) ?></div>
<?php endif; ?>
<form method="post" action="/profile.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="change_password">
<div>
<label for="new_password">Neues Passwort</label>
<input type="password" id="new_password" name="new_password" required>
<div class="field-hint">Mindestens 8 Zeichen.</div>
</div>
<div>
<label for="new_password_confirm">Neues Passwort wiederholen</label>
<input type="password" id="new_password_confirm" name="new_password_confirm" required>
</div>
<button type="submit">Passwort speichern</button>
</form>
</div>
<div class="panel stat-card">