LoginController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 24
c 2
b 0
f 0
dl 0
loc 53
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 39 5
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Auth;
7
8
use Illuminate\Auth\Events\Lockout;
9
use App\Http\Controllers\Controller;
10
use App\Http\Requests\Auth\LoginRequest;
11
use Illuminate\Auth\Events\Failed;
12
13
class LoginController extends Controller
14
{
15
    protected $loginAttempts = 4;
16
    protected $lockoutTimer  = 10;
17
18
    public function __construct()
19 14
    {
20
        //  To help prevent bots, we will not allow more than 50 login attempts within a two hour period
21 14
        $this->middleware('throttle:50,120');
22 14
    }
23 14
24 14
    /**
25
     *  Attempt to log a user in
26
     */
27 8
    public function __invoke(LoginRequest $request)
28
    {
29 8
        //  Determine if the user has tried to log in too many times already
30
        if(session('failed_login') > $this->loginAttempts)
31
        {
32
            $timeout = session('timeout') ? session('timeout') : Carbon::now()->addMinutes($this->lockoutTimer);
33
            if($timeout > Carbon::now())
34
            {
35
                session([
36
                    'timeout' => $timeout,
37
                ]);
38
                $request->session()->increment('failed_login');
39
                event(new Lockout($request));
40
41
                return back()->withErrors([
42
                    'username' => 'You have attempted to log in too many times.  Please wait '.$this->lockoutTimer.' minutes before trying again.'
43
                ]);
44
            }
45
46
            //  If the user has passed the 10 minute timeout, they can attempt a login again
47
            $request->session()->forget(['failed_login', 'timeout']);
48
        }
49
50
        $user = [
51
            'username' => $request->username,
52
            'password' => $request->password,
53
        ];
54
55
        //  Successful authentication re-routes to the dashboard, or the page that the user tried to visit
56
        if(Auth::attempt($user, $request->remember))
57
        {
58
            $request->session()->forget(['failed_login', 'timeout']);
59
            $request->session()->regenerate();
60
            return redirect()->intended('dashboard');
61
        }
62
63
        $request->session()->increment('failed_login');
64
        event(new Failed(null, null, $user));
65
        return back()->withErrors(['username' => 'Your username or password does not match our records']);
66
    }
67
}
68