Passed
Push — master ( 30f336...ab8928 )
by Brian
14:05
created

NewPasswordController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 30 2
A create() 0 3 1
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\View\View;
14
15
class NewPasswordController extends Controller
16
{
17
    /**
18
     * Display the password reset view.
19
     */
20
    public function create(Request $request): View
21
    {
22
        return view('auth.reset-password', ['request' => $request]);
23
    }
24
25
    /**
26
     * Handle an incoming new password request.
27
     *
28
     * @throws \Illuminate\Validation\ValidationException
29
     */
30
    public function store(Request $request): RedirectResponse
31
    {
32
        $request->validate([
33
            'token' => ['required'],
34
            'email' => ['required', 'email'],
35
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
36
        ]);
37
38
        // Here we will attempt to reset the user's password. If it is successful we
39
        // will update the password on an actual user model and persist it to the
40
        // database. Otherwise we will parse the error and return the response.
41
        $status = Password::reset(
42
            $request->only('email', 'password', 'password_confirmation', 'token'),
43
            function ($user) use ($request) {
44
                $user->forceFill([
45
                    'password' => Hash::make($request->password),
46
                    'remember_token' => Str::random(60),
47
                ])->save();
48
49
                event(new PasswordReset($user));
50
            }
51
        );
52
53
        // If the password was successfully reset, we will redirect the user back to
54
        // the application's home authenticated view. If there is an error we can
55
        // redirect them back to where they came from with their error message.
56
        return $status == Password::PASSWORD_RESET
57
                    ? redirect()->route('login')->with('status', __($status))
58
                    : back()->withInput($request->only('email'))
59
                            ->withErrors(['email' => __($status)]);
60
    }
61
}
62