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