Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
21 / 24 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| NewPasswordController | |
87.50% |
21 / 24 |
|
50.00% |
1 / 2 |
3.02 | |
0.00% |
0 / 1 |
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| store | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Auth; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use Illuminate\Auth\Events\PasswordReset; |
| 7 | use Illuminate\Http\RedirectResponse; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Hash; |
| 10 | use Illuminate\Support\Facades\Password; |
| 11 | use Illuminate\Support\Str; |
| 12 | use Illuminate\Validation\Rules; |
| 13 | use Illuminate\Validation\ValidationException; |
| 14 | use Inertia\Inertia; |
| 15 | use Inertia\Response; |
| 16 | |
| 17 | class NewPasswordController extends Controller |
| 18 | { |
| 19 | /** |
| 20 | * Display the password reset view. |
| 21 | */ |
| 22 | public function create(Request $request): Response |
| 23 | { |
| 24 | return Inertia::render('Auth/ResetPassword', [ |
| 25 | 'email' => $request->email, |
| 26 | 'token' => $request->route('token'), |
| 27 | ]); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Handle an incoming new password request. |
| 32 | * |
| 33 | * @throws \Illuminate\Validation\ValidationException |
| 34 | */ |
| 35 | public function store(Request $request): RedirectResponse |
| 36 | { |
| 37 | $request->validate([ |
| 38 | 'token' => 'required', |
| 39 | 'email' => 'required|email', |
| 40 | 'password' => ['required', 'confirmed', Rules\Password::defaults()], |
| 41 | ]); |
| 42 | |
| 43 | // Here we will attempt to reset the user's password. If it is successful we |
| 44 | // will update the password on an actual user model and persist it to the |
| 45 | // database. Otherwise we will parse the error and return the response. |
| 46 | $status = Password::reset( |
| 47 | $request->only('email', 'password', 'password_confirmation', 'token'), |
| 48 | function ($user) use ($request) { |
| 49 | $user->forceFill([ |
| 50 | 'password' => Hash::make($request->password), |
| 51 | 'remember_token' => Str::random(60), |
| 52 | ])->save(); |
| 53 | |
| 54 | event(new PasswordReset($user)); |
| 55 | } |
| 56 | ); |
| 57 | |
| 58 | // If the password was successfully reset, we will redirect the user back to |
| 59 | // the application's home authenticated view. If there is an error we can |
| 60 | // redirect them back to where they came from with their error message. |
| 61 | if ($status == Password::PASSWORD_RESET) { |
| 62 | return redirect()->route('login')->with('status', __($status)); |
| 63 | } |
| 64 | |
| 65 | throw ValidationException::withMessages([ |
| 66 | 'email' => [trans($status)], |
| 67 | ]); |
| 68 | } |
| 69 | } |