Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewPasswordController
87.50% covered (warning)
87.50%
21 / 24
50.00% covered (danger)
50.00%
1 / 2
3.02
0.00% covered (danger)
0.00%
0 / 1
 create
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 store
85.00% covered (warning)
85.00%
17 / 20
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3namespace App\Http\Controllers\Auth;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Auth\Events\PasswordReset;
7use Illuminate\Http\RedirectResponse;
8use Illuminate\Http\Request;
9use Illuminate\Support\Facades\Hash;
10use Illuminate\Support\Facades\Password;
11use Illuminate\Support\Str;
12use Illuminate\Validation\Rules;
13use Illuminate\Validation\ValidationException;
14use Inertia\Inertia;
15use Inertia\Response;
16
17class 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}