LoginController::username()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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