LoginController::login()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Events\LogedUser;
6
use App\Http\Controllers\Controller;
7
use Illuminate\Foundation\Auth\AuthenticatesUsers;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Log;
11
12
class LoginController extends Controller
13
{
14
    /*
15
    |--------------------------------------------------------------------------
16
    | Login Controller
17
    |--------------------------------------------------------------------------
18
    |
19
    | This controller handles authenticating users for the application and
20
    | redirecting them to your home screen. The controller uses a trait
21
    | to conveniently provide its functionality to your applications.
22
    |
23
    */
24
25
    use AuthenticatesUsers {
26
        attemptLogin as attemptLoginAtAuthenticatesUsers;
27
    }
28
29
    /**
30
     * Handle a login request to the application.
31
     *
32
     * @param \Illuminate\Http\Request $request
33
     *
34
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
35
     */
36
    public function login(Request $request)
37
    {
38
        $this->validateLogin($request);
39
40
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
41
        // the login attempts for this application. We'll key this by the username and
42
        // the IP address of the client making these requests into this application.
43
        if ($this->hasTooManyLoginAttempts($request)) {
44
            $this->fireLockoutEvent($request);
45
46
            return $this->sendLockoutResponse($request);
47
        }
48
49
        if ($this->attemptLogin($request)) {
50
            event(new LogedUser(Auth::user()));
51
52
            return $this->sendLoginResponse($request);
53
        }
54
55
        // If the login attempt was unsuccessful we will increment the number of attempts
56
        // to login and redirect the user back to the login form. Of course, when this
57
        // user surpasses their maximum number of attempts they will get locked out.
58
        $this->incrementLoginAttempts($request);
59
60
        return $this->sendFailedLoginResponse($request);
61
    }
62
63
    /**
64
     * Show the application's login form.
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function showLoginForm()
69
    {
70
        return view('adminlte::auth.login');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('adminlte::auth.login') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
71
    }
72
73
    /**
74
     * Where to redirect users after login.
75
     *
76
     * @var string
77
     */
78
    protected $redirectTo = '/home';
79
80
    /**
81
     * Create a new controller instance.
82
     *
83
     * @return void
84
     */
85
    public function __construct()
86
    {
87
        $this->middleware('guest', ['except' => 'logout']);
88
    }
89
90
    /**
91
     * Returns field name to use at login.
92
     *
93
     * @return string
94
     */
95
    public function username()
96
    {
97
        return config('auth.providers.users.field', 'email');
98
    }
99
100
    /**
101
     * Attempt to log the user into the application.
102
     *
103
     * @param \Illuminate\Http\Request $request
104
     *
105
     * @return bool
106
     */
107
    protected function attemptLogin(Request $request)
108
    {
109
        if ($this->username() === 'email') {
110
            return $this->attemptLoginAtAuthenticatesUsers($request);
111
        }
112
        if (!$this->attemptLoginAtAuthenticatesUsers($request)) {
113
            return $this->attempLoginUsingUsernameAsAnEmail($request);
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Attempt to log the user into application using username as an email.
121
     *
122
     * @param \Illuminate\Http\Request $request
123
     *
124
     * @return bool
125
     */
126
    protected function attempLoginUsingUsernameAsAnEmail(Request $request)
127
    {
128
        return $this->guard()->attempt(
129
            ['email' => $request->input('username'), 'password' => $request->input('password')],
130
            $request->has('remember'));
131
    }
132
}
133