Passwort-vergessen-Funktion ergänzen
This commit is contained in:
parent
5caf55f4a6
commit
e3b9988cbc
9 changed files with 285 additions and 5 deletions
|
|
@ -39,6 +39,119 @@ class AuthController
|
|||
redirect('/');
|
||||
}
|
||||
|
||||
public function showLinkRequestForm(): void
|
||||
{
|
||||
view('auth.passwords.email');
|
||||
}
|
||||
|
||||
public function sendResetLinkEmail(): void
|
||||
{
|
||||
validate([
|
||||
'email' => 'required|email',
|
||||
]);
|
||||
|
||||
$email = trim((string) input('email'));
|
||||
$user = User::byEmail($email);
|
||||
|
||||
if ($user !== null) {
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$hash = hash('sha256', $token);
|
||||
|
||||
db()->execute('DELETE FROM `password_resets` WHERE email = ?', [$email]);
|
||||
db()->execute(
|
||||
'INSERT INTO `password_resets` (email, token, created_at) VALUES (?, ?, ?)',
|
||||
[$email, $hash, now()]
|
||||
);
|
||||
|
||||
$link = app_base_url() . '/password/reset/' . $token . '?email=' . rawurlencode($email);
|
||||
|
||||
$body = "Hallo " . $user->name . ",\n\n"
|
||||
. "du hast ein neues Passwort für die Motorrad-IG angefordert.\n\n"
|
||||
. "Über diesen Link kannst du ein neues Passwort setzen:\n"
|
||||
. $link . "\n\n"
|
||||
. "Der Link ist 60 Minuten gültig und kann nur einmal verwendet werden.\n\n"
|
||||
. "Wenn du kein neues Passwort angefordert hast, ignoriere diese E-Mail.\n\n"
|
||||
. "Viele Grüße\n"
|
||||
. "Motorrad-IG\n";
|
||||
|
||||
if (!send_mail($email, 'Passwort zurücksetzen', $body, (string) config('mail.reply_to'))) {
|
||||
app_log('warning', 'Passwort-Reset-E-Mail konnte nicht versendet werden', [
|
||||
'email' => $email,
|
||||
]);
|
||||
} else {
|
||||
app_log('info', 'Passwort-Reset-E-Mail versendet', [
|
||||
'user_id' => (int) $user->id,
|
||||
'email' => $email,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
flash_set('status', 'Wenn ein Konto mit dieser E-Mail-Adresse existiert, wurde ein Link zum Zurücksetzen versendet.');
|
||||
redirect('/password/reset');
|
||||
}
|
||||
|
||||
public function showResetForm(string $token): void
|
||||
{
|
||||
view('auth.passwords.reset', [
|
||||
'token' => $token,
|
||||
'email' => (string) input('email'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function resetPassword(): void
|
||||
{
|
||||
validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required|min:8',
|
||||
]);
|
||||
|
||||
$email = trim((string) input('email'));
|
||||
$token = (string) input('token');
|
||||
$password = (string) input('password');
|
||||
$confirmation = (string) input('password_confirmation');
|
||||
|
||||
if ($password !== $confirmation) {
|
||||
$_SESSION['_errors']['password'][] = 'Die Passwörter stimmen nicht überein.';
|
||||
$_SESSION['_old'] = $this->withoutResetSecrets($_POST);
|
||||
back();
|
||||
}
|
||||
|
||||
$user = User::byEmail($email);
|
||||
$reset = db()->fetchOne(
|
||||
'SELECT * FROM `password_resets`
|
||||
WHERE email = ? AND token = ? AND created_at >= (NOW() - INTERVAL 60 MINUTE)
|
||||
LIMIT 1',
|
||||
[$email, hash('sha256', $token)]
|
||||
);
|
||||
|
||||
if ($user === null || $reset === null) {
|
||||
$_SESSION['_errors']['email'][] = 'Der Link ist ungültig oder abgelaufen. Bitte fordere einen neuen Link an.';
|
||||
$_SESSION['_old'] = $this->withoutResetSecrets($_POST);
|
||||
back();
|
||||
}
|
||||
|
||||
$user->password = password_hash($password, PASSWORD_BCRYPT);
|
||||
$user->updated_at = now();
|
||||
$user->save();
|
||||
|
||||
db()->execute('DELETE FROM `password_resets` WHERE email = ?', [$email]);
|
||||
|
||||
app_log('info', 'Passwort zurückgesetzt', [
|
||||
'user_id' => (int) $user->id,
|
||||
'email' => $email,
|
||||
]);
|
||||
|
||||
flash_set('status', 'Dein Passwort wurde geändert. Bitte melde dich an.');
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
private function withoutResetSecrets(array $input): array
|
||||
{
|
||||
unset($input['_token'], $input['token'], $input['password'], $input['password_confirmation']);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
public function showRegisterForm(): void
|
||||
{
|
||||
view('auth.register');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue