Passed
Push — master ( c12a7e...1e568f )
by Brian
02:51
created

NewPasswordController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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 0
cts 20
cp 0
crap 6
rs 9.7333
1
<?php
2
3
namespace App\Http\Controllers\Admin\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('admin.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 admin'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::broker('admins')->reset(
42
            $request->only('email', 'password', 'password_confirmation', 'token'),
43
            function ($admin) use ($request) {
44
                $admin->forceFill([
45
                    'password' => Hash::make($request->password),
46
                    'remember_token' => Str::random(60),
47
                ])->save();
48
49
                event(new PasswordReset($admin));
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('admin.login')->with('status', __($status))
58
                    : back()->withInput($request->only('email'))
59
                            ->withErrors(['email' => __($status)]);
60
    }
61
}
62