126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Mail\PasswordResetMail;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\View\View;
|
|
use Throwable;
|
|
|
|
class ForgotPasswordController extends Controller
|
|
{
|
|
/**
|
|
* Display the form to request a password reset link.
|
|
*/
|
|
public function showLinkRequestForm(): View
|
|
{
|
|
return view('auth.forgot-password');
|
|
}
|
|
|
|
/**
|
|
* Send a reset link to the given user.
|
|
*/
|
|
public function sendResetLinkEmail(Request $request): RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'email' => 'required|email',
|
|
]);
|
|
|
|
$user = User::where('email', $request->input('email'))->first();
|
|
|
|
// Always show friendly message to prevent email enumeration
|
|
if (! $user) {
|
|
return back()->with('success', 'Als dit e-mailadres bij ons bekend is, hebben we zojuist een herstellink verstuurd.');
|
|
}
|
|
|
|
$token = Str::random(64);
|
|
|
|
DB::table('password_reset_tokens')->updateOrInsert(
|
|
['email' => $user->email],
|
|
[
|
|
'token' => Hash::make($token),
|
|
'created_at' => Carbon::now(),
|
|
]
|
|
);
|
|
|
|
$resetUrl = route('password.reset', [
|
|
'token' => $token,
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
try {
|
|
Mail::to($user->email)->send(new PasswordResetMail($user, $resetUrl));
|
|
} catch (Throwable $e) {
|
|
report($e);
|
|
}
|
|
|
|
return back()->with('success', 'Als dit e-mailadres bij ons bekend is, hebben we zojuist een herstellink verstuurd.');
|
|
}
|
|
|
|
/**
|
|
* Display the password reset view for the given token.
|
|
*/
|
|
public function showResetForm(Request $request, string $token): View
|
|
{
|
|
return view('auth.reset-password', [
|
|
'token' => $token,
|
|
'email' => $request->input('email'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Reset the given user's password.
|
|
*/
|
|
public function reset(Request $request): RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'token' => 'required|string',
|
|
'email' => 'required|email',
|
|
'password' => 'required|string|min:8|confirmed',
|
|
], [
|
|
'password.min' => 'Het nieuwe wachtwoord moet minimaal 8 tekens lang zijn.',
|
|
'password.confirmed' => 'De wachtwoorden komen niet met elkaar overeen.',
|
|
]);
|
|
|
|
$record = DB::table('password_reset_tokens')
|
|
->where('email', $request->input('email'))
|
|
->first();
|
|
|
|
if (! $record || ! Hash::check($request->input('token'), $record->token)) {
|
|
throw ValidationException::withMessages([
|
|
'email' => ['Deze herstellink is ongeldig of verlopen.'],
|
|
]);
|
|
}
|
|
|
|
if (Carbon::parse($record->created_at)->addMinutes(60)->isPast()) {
|
|
DB::table('password_reset_tokens')->where('email', $request->input('email'))->delete();
|
|
throw ValidationException::withMessages([
|
|
'email' => ['Deze herstellink is verlopen. Vraag alsjeblieft een nieuwe link aan.'],
|
|
]);
|
|
}
|
|
|
|
$user = User::where('email', $request->input('email'))->firstOrFail();
|
|
$user->update([
|
|
'password' => Hash::make($request->input('password')),
|
|
]);
|
|
|
|
DB::table('password_reset_tokens')->where('email', $request->input('email'))->delete();
|
|
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('account.index')
|
|
->with('success', 'Jouw wachtwoord is succesvol gewijzigd! Je bent direct ingelogd.');
|
|
}
|
|
}
|
|
|