133 lines
3.7 KiB
PHP
133 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
use Illuminate\Contracts\Auth\CanResetPassword;
|
|
use Illuminate\Contracts\Auth\PasswordBroker;
|
|
use Illuminate\Contracts\Auth\StatefulGuard;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Password;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rules;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response as InertiaResponse;
|
|
|
|
/**
|
|
* Handles resetting a password from an emailed token.
|
|
*
|
|
* This behaviour came from laravel/ui's ResetsPasswords trait, inlined here so
|
|
* the package is no longer a dependency.
|
|
*/
|
|
class ResetPasswordController extends Controller
|
|
{
|
|
/**
|
|
* Where to redirect users after resetting their password.
|
|
*/
|
|
protected string $redirectTo = '/';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware('guest');
|
|
}
|
|
|
|
/**
|
|
* Display the password reset form for the given token.
|
|
*/
|
|
public function showResetForm(Request $request, ?string $token = null): InertiaResponse
|
|
{
|
|
return Inertia::render('auth/ResetPassword', [
|
|
'token' => $token,
|
|
'email' => $request->email,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Reset the given user's password.
|
|
*/
|
|
public function reset(Request $request): RedirectResponse|JsonResponse
|
|
{
|
|
$request->validate([
|
|
'token' => 'required',
|
|
'email' => 'required|email',
|
|
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
|
]);
|
|
|
|
$response = $this->broker()->reset(
|
|
$request->only('email', 'password', 'password_confirmation', 'token'),
|
|
fn (CanResetPassword $user, string $password) => $this->resetPassword($user, $password)
|
|
);
|
|
|
|
return $response === Password::PASSWORD_RESET
|
|
? $this->sendResetResponse($request, $response)
|
|
: $this->sendResetFailedResponse($request, $response);
|
|
}
|
|
|
|
/**
|
|
* Reset the given user's password.
|
|
*
|
|
* The password is assigned unhashed because the User model hashes it.
|
|
*/
|
|
protected function resetPassword(CanResetPassword $user, string $password): void
|
|
{
|
|
$user->password = $password;
|
|
|
|
$user->setRememberToken(Str::random(60));
|
|
|
|
$user->save();
|
|
|
|
event(new PasswordReset($user));
|
|
|
|
$this->guard()->login($user);
|
|
}
|
|
|
|
/**
|
|
* Get the response for a successful password reset.
|
|
*/
|
|
protected function sendResetResponse(Request $request, string $response): RedirectResponse|JsonResponse
|
|
{
|
|
if ($request->wantsJson()) {
|
|
return new JsonResponse(['message' => trans($response)], 200);
|
|
}
|
|
|
|
return redirect($this->redirectTo)->with('status', trans($response));
|
|
}
|
|
|
|
/**
|
|
* Get the response for a failed password reset.
|
|
*
|
|
* @throws ValidationException
|
|
*/
|
|
protected function sendResetFailedResponse(Request $request, string $response): RedirectResponse
|
|
{
|
|
if ($request->wantsJson()) {
|
|
throw ValidationException::withMessages(['email' => [trans($response)]]);
|
|
}
|
|
|
|
return back()
|
|
->withInput($request->only('email'))
|
|
->withErrors(['email' => trans($response)]);
|
|
}
|
|
|
|
/**
|
|
* Get the broker to be used during password reset.
|
|
*/
|
|
public function broker(): PasswordBroker
|
|
{
|
|
return Password::broker();
|
|
}
|
|
|
|
/**
|
|
* Get the guard to be used during password reset.
|
|
*/
|
|
protected function guard(): StatefulGuard
|
|
{
|
|
return Auth::guard();
|
|
}
|
|
}
|