1 | <?php |
||||
2 | |||||
3 | namespace Devpri\Tinre\Http\Controllers\Auth; |
||||
4 | |||||
5 | use Devpri\Tinre\Http\Controllers\Controller; |
||||
6 | use Devpri\Tinre\Traits\ThrottlesLogins; |
||||
7 | use Illuminate\Http\Request; |
||||
8 | use Illuminate\Http\Response; |
||||
9 | use Illuminate\Support\Facades\Auth; |
||||
10 | use Illuminate\Validation\ValidationException; |
||||
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. |
||||
21 | | |
||||
22 | */ |
||||
23 | |||||
24 | use ThrottlesLogins; |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
25 | |||||
26 | /** |
||||
27 | * Where to redirect users after login. |
||||
28 | * |
||||
29 | * @var string |
||||
30 | */ |
||||
31 | 1 | public function redirectTo() |
|||
32 | { |
||||
33 | 1 | return route('dashboard'); |
|||
34 | } |
||||
35 | |||||
36 | /** |
||||
37 | * Create a new controller instance. |
||||
38 | * |
||||
39 | * @return void |
||||
40 | */ |
||||
41 | 5 | public function __construct() |
|||
42 | { |
||||
43 | 5 | $this->middleware('guest')->except('logout'); |
|||
44 | 5 | } |
|||
45 | |||||
46 | /** |
||||
47 | * Show the application's login form. |
||||
48 | * |
||||
49 | * @return \Illuminate\Http\Response |
||||
50 | */ |
||||
51 | 1 | public function showLoginForm() |
|||
52 | { |
||||
53 | 1 | return view('tinre::auth.login'); |
|||
0 ignored issues
–
show
|
|||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * Handle a login request to the application. |
||||
58 | * |
||||
59 | * @param \Illuminate\Http\Request $request |
||||
60 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
||||
61 | * |
||||
62 | * @throws \Illuminate\Validation\ValidationException |
||||
63 | */ |
||||
64 | 3 | public function login(Request $request) |
|||
65 | { |
||||
66 | 3 | $this->validateLogin($request); |
|||
67 | |||||
68 | // If the class is using the ThrottlesLogins trait, we can automatically throttle |
||||
69 | // the login attempts for this application. We'll key this by the username and |
||||
70 | // the IP address of the client making these requests into this application. |
||||
71 | if ( |
||||
72 | 3 | method_exists($this, 'hasTooManyLoginAttempts') && |
|||
73 | 3 | $this->hasTooManyLoginAttempts($request) |
|||
74 | ) { |
||||
75 | $this->fireLockoutEvent($request); |
||||
76 | |||||
77 | return $this->sendLockoutResponse($request); |
||||
78 | } |
||||
79 | |||||
80 | 3 | if ($this->attemptLogin($request)) { |
|||
81 | 1 | return $this->sendLoginResponse($request); |
|||
82 | } |
||||
83 | |||||
84 | // If the login attempt was unsuccessful we will increment the number of attempts |
||||
85 | // to login and redirect the user back to the login form. Of course, when this |
||||
86 | // user surpasses their maximum number of attempts they will get locked out. |
||||
87 | 2 | $this->incrementLoginAttempts($request); |
|||
88 | |||||
89 | 2 | return $this->sendFailedLoginResponse($request); |
|||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * Log the user out of the application. |
||||
94 | * |
||||
95 | * @param \Illuminate\Http\Request $request |
||||
96 | * @return \Illuminate\Http\Response |
||||
97 | */ |
||||
98 | public function logout(Request $request) |
||||
99 | { |
||||
100 | $this->guard()->logout(); |
||||
101 | |||||
102 | $request->session()->invalidate(); |
||||
103 | |||||
104 | $request->session()->regenerateToken(); |
||||
105 | |||||
106 | return $request->wantsJson() |
||||
0 ignored issues
–
show
|
|||||
107 | ? new Response('', 204) |
||||
108 | : redirect()->route('login'); |
||||
109 | } |
||||
110 | |||||
111 | /** |
||||
112 | * Validate the user login request. |
||||
113 | * |
||||
114 | * @param \Illuminate\Http\Request $request |
||||
115 | * @return void |
||||
116 | * |
||||
117 | * @throws \Illuminate\Validation\ValidationException |
||||
118 | */ |
||||
119 | 3 | protected function validateLogin(Request $request) |
|||
120 | { |
||||
121 | 3 | $request->validate([ |
|||
122 | 3 | 'email' => ['required', 'email'], |
|||
123 | 'password' => ['required', 'string'], |
||||
124 | ]); |
||||
125 | 3 | } |
|||
126 | |||||
127 | /** |
||||
128 | * Attempt to log the user into the application. |
||||
129 | * |
||||
130 | * @param \Illuminate\Http\Request $request |
||||
131 | * @return bool |
||||
132 | */ |
||||
133 | 3 | protected function attemptLogin(Request $request) |
|||
134 | { |
||||
135 | 3 | return $this->guard()->attempt( |
|||
136 | 3 | $this->credentials($request), |
|||
137 | 3 | $request->filled('remember') |
|||
138 | ); |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * Get the needed authorization credentials from the request. |
||||
143 | * |
||||
144 | * @param \Illuminate\Http\Request $request |
||||
145 | * @return array |
||||
146 | */ |
||||
147 | 3 | protected function credentials(Request $request) |
|||
148 | { |
||||
149 | 3 | return $request->only('email', 'password'); |
|||
150 | } |
||||
151 | |||||
152 | /** |
||||
153 | * Send the response after the user was authenticated. |
||||
154 | * |
||||
155 | * @param \Illuminate\Http\Request $request |
||||
156 | * @return \Illuminate\Http\Response |
||||
157 | */ |
||||
158 | 1 | protected function sendLoginResponse(Request $request) |
|||
159 | { |
||||
160 | 1 | $request->session()->regenerate(); |
|||
161 | |||||
162 | 1 | $this->clearLoginAttempts($request); |
|||
163 | |||||
164 | 1 | $user = $this->guard()->user(); |
|||
165 | |||||
166 | 1 | if (! $user->active) { |
|||
0 ignored issues
–
show
|
|||||
167 | $this->guard()->logout(); |
||||
168 | throw ValidationException::withMessages([ |
||||
169 | 'email' => [trans('Your account has beem disabled.')], |
||||
170 | ]); |
||||
171 | } |
||||
172 | |||||
173 | 1 | return $request->wantsJson() |
|||
0 ignored issues
–
show
|
|||||
174 | ? new Response('', 204) |
||||
175 | 1 | : redirect($this->redirectTo()); |
|||
176 | } |
||||
177 | |||||
178 | /** |
||||
179 | * Get the failed login response instance. |
||||
180 | * |
||||
181 | * @param \Illuminate\Http\Request $request |
||||
182 | * @return \Symfony\Component\HttpFoundation\Response |
||||
183 | * |
||||
184 | * @throws \Illuminate\Validation\ValidationException |
||||
185 | */ |
||||
186 | 2 | protected function sendFailedLoginResponse(Request $request) |
|||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||
187 | { |
||||
188 | 2 | throw ValidationException::withMessages([ |
|||
189 | 2 | 'email' => [trans('auth.failed')], |
|||
190 | ]); |
||||
191 | } |
||||
192 | |||||
193 | /** |
||||
194 | * Get the guard to be used during authentication. |
||||
195 | * |
||||
196 | * @return \Illuminate\Contracts\Auth\StatefulGuard |
||||
197 | */ |
||||
198 | 3 | protected function guard() |
|||
199 | { |
||||
200 | 3 | return Auth::guard(); |
|||
201 | } |
||||
202 | } |
||||
203 |