Passed
Push — master ( 3e77af...7c612c )
by Brian
02:49
created

NewPasswordController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Modules\Admins\Http\Controllers\Auth;
4
5
use App\Modules\Admins\Http\Controllers\Controller;
6
use Illuminate\Auth\Events\PasswordReset;
7
use Illuminate\Http\JsonResponse;
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\Validation\ValidationException;
14
15
class NewPasswordController extends Controller
16
{
17
    /**
18
     * Handle an incoming new password request.
19
     *
20
     * @throws \Illuminate\Validation\ValidationException
21
     */
22
    public function store(Request $request): JsonResponse
23
    {
24
        $request->validate([
25
            'token' => ['required'],
26
            'email' => ['required', 'email'],
27
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
28
        ]);
29
30
        // Here we will attempt to reset the user's password. If it is successful we
31
        // will update the password on an actual user model and persist it to the
32
        // database. Otherwise we will parse the error and return the response.
33
        $status = Password::broker('admins')->reset(
34
            $request->only('email', 'password', 'password_confirmation', 'token'),
35
            function ($user) use ($request) {
36
                $user->forceFill([
37
                    'password' => Hash::make($request->password),
38
                    'remember_token' => Str::random(60),
39
                ])->save();
40
41
                event(new PasswordReset($user));
42
            }
43
        );
44
45
        if ($status != Password::PASSWORD_RESET) {
46
            throw ValidationException::withMessages([
47
                'email' => [__($status)],
48
            ]);
49
        }
50
51
        return response()->json(['status' => __($status)]);
52
    }
53
}
54