Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

ResetPasswordController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 63
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Auth;
4
5
use Illuminate\Support\Facades\Password;
6
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Foundation\Auth\ResetsPasswords;
9
use Illuminate\Support\Facades\Auth;
10
use Thinktomorrow\Chief\Authorization\ChiefPasswordBroker;
11
use Thinktomorrow\Chief\Authorization\ChiefPasswordBrokerResolver;
12
13
class ResetPasswordController extends Controller
14
{
15
    /*
16
    |--------------------------------------------------------------------------
17
    | Password Reset Controller
18
    |--------------------------------------------------------------------------
19
    |
20
    | This controller is responsible for handling password reset requests
21
    | and uses a simple trait to include this behavior. You're free to
22
    | explore this trait and override any methods you wish to tweak.
23
    |
24
    */
25
26
    use ResetsPasswords;
27
28
    public function __construct()
29
    {
30
        $this->middleware('chief-guest');
31
    }
32
33
    public function showResetForm(Request $request, $token = null)
34
    {
35
        return view('chief::auth.passwords.reset')->with(
36
            ['token' => $token, 'email' => $request->email]
37
        );
38
    }
39
40
    protected function guard()
41
    {
42
        return Auth::guard('chief');
43
    }
44
45
    protected function broker()
46
    {
47
        return (new ChiefPasswordBrokerResolver(app()))->resolve();
48
    }
49
50
    public function redirectTo()
51
    {
52
        return route('chief.back.dashboard');
53
    }
54
55
    // Override the reset method because chief uses different lang keys and
56
    // laravel internals expects this to be of a specific value.
57
    public function reset(Request $request)
58
    {
59
        $request->validate($this->rules(), $this->validationErrorMessages());
60
61
        // Here we will attempt to reset the user's password. If it is successful we
62
        // will update the password on an actual user model and persist it to the
63
        // database. Otherwise we will parse the error and return the response.
64
        $response = $this->broker()->reset(
65
            $this->credentials($request), function ($user, $password) {
66
                $this->resetPassword($user, $password);
67
            }
68
        );
69
70
        // If the password was successfully reset, we will redirect the user back to
71
        // the application's home authenticated view. If there is an error we can
72
        // redirect them back to where they came from with their error message.
73
        return $response == ChiefPasswordBroker::PASSWORD_RESET
74
            ? $this->sendResetResponse($request, $response)
75
            : $this->sendResetFailedResponse($request, $response);
76
    }
77
}
78