NewPasswordController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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