Passed
Push — develop ( 4cbaa0...09ba9e )
by Ngoding
05:32
created

NewPasswordController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 30
ccs 14
cts 15
cp 0.9333
crap 2.0011
rs 9.7333
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\Request;
8
use Illuminate\Support\Facades\Event;
9
use Illuminate\Support\Facades\Hash;
10
use Illuminate\Support\Facades\Password;
11
use Illuminate\Support\Str;
12
use Illuminate\Validation\Rules;
13
14
class NewPasswordController extends Controller
15
{
16
    /**
17
     * Display the password reset view.
18
     *
19
     * @param  \Illuminate\Http\Request  $request
20
     * @return \Illuminate\View\View
21
     */
22 1
    public function create(Request $request)
23
    {
24 1
        return view('auth.reset-password', ['request' => $request]);
25
    }
26
27
    /**
28
     * Handle an incoming new password request.
29
     *
30
     * @param  \Illuminate\Http\Request  $request
31
     * @return \Illuminate\Http\RedirectResponse
32
     *
33
     * @throws \Illuminate\Validation\ValidationException
34
     */
35 1
    public function store(Request $request)
36
    {
37 1
        $request->validate([
38
            'token' => ['required'],
39
            'email' => ['required', 'email'],
40 1
            '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 1
        $status = Password::reset(
47 1
            $request->only('email', 'password', 'password_confirmation', 'token'),
48 1
            function ($user) use ($request) {
49 1
                $user->forceFill([
50 1
                    'password' => Hash::make($request->password),
51 1
                    'remember_token' => Str::random(60),
52 1
                ])->save();
53
54 1
                Event::dispatch(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 1
        return $status == Password::PASSWORD_RESET
62 1
            ? redirect()->route('login')->with('status', __($status))
63
            : back()->withInput($request->only('email'))
64 1
                    ->withErrors(['email' => __($status)]);
65
    }
66
}
67