|
16 | 16 | class AuthController extends Controller |
17 | 17 | { |
18 | 18 | /** |
19 | | - * Login page. |
| 19 | + * Show the login page. |
20 | 20 | * |
21 | 21 | * @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View |
22 | 22 | */ |
23 | 23 | public function getLogin() |
24 | 24 | { |
25 | | - if (!Auth::guard('admin')->guest()) { |
26 | | - return redirect(config('admin.route.prefix')); |
| 25 | + if ($this->guard()->check()) { |
| 26 | + return redirect($this->redirectPath()); |
27 | 27 | } |
28 | 28 |
|
29 | 29 | return view('admin::login'); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | /** |
| 33 | + * Handle a login request. |
| 34 | + * |
33 | 35 | * @param Request $request |
34 | 36 | * |
35 | 37 | * @return mixed |
36 | 38 | */ |
37 | 39 | public function postLogin(Request $request) |
38 | 40 | { |
39 | | - $credentials = $request->only(['username', 'password']); |
| 41 | + $credentials = $request->only([$this->username(), 'password']); |
40 | 42 |
|
| 43 | + /** @var \Illuminate\Validation\Validator $validator */ |
41 | 44 | $validator = Validator::make($credentials, [ |
42 | | - 'username' => 'required', 'password' => 'required', |
| 45 | + $this->username() => 'required', |
| 46 | + 'password' => 'required', |
43 | 47 | ]); |
44 | 48 |
|
45 | 49 | if ($validator->fails()) { |
46 | | - return Redirect::back()->withInput()->withErrors($validator); |
| 50 | + return back()->withInput()->withErrors($validator); |
47 | 51 | } |
48 | 52 |
|
49 | | - if (Auth::guard('admin')->attempt($credentials)) { |
50 | | - admin_toastr(trans('admin.login_successful')); |
51 | | - |
52 | | - return redirect()->intended(config('admin.route.prefix')); |
| 53 | + if ($this->guard()->attempt($credentials)) { |
| 54 | + return $this->sendLoginResponse($request); |
53 | 55 | } |
54 | 56 |
|
55 | | - return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]); |
| 57 | + return back()->withInput()->withErrors([ |
| 58 | + $this->username() => $this->getFailedLoginMessage(), |
| 59 | + ]); |
56 | 60 | } |
57 | 61 |
|
58 | 62 | /** |
59 | 63 | * User logout. |
60 | 64 | * |
61 | 65 | * @return Redirect |
62 | 66 | */ |
63 | | - public function getLogout() |
| 67 | + public function getLogout(Request $request) |
64 | 68 | { |
65 | | - Auth::guard('admin')->logout(); |
| 69 | + $this->guard()->logout(); |
66 | 70 |
|
67 | | - session()->forget('url.intented'); |
| 71 | + $request->session()->invalidate(); |
68 | 72 |
|
69 | 73 | return redirect(config('admin.route.prefix')); |
70 | 74 | } |
@@ -143,4 +147,54 @@ protected function getFailedLoginMessage() |
143 | 147 | ? trans('auth.failed') |
144 | 148 | : 'These credentials do not match our records.'; |
145 | 149 | } |
| 150 | + |
| 151 | + /** |
| 152 | + * Get the post login redirect path. |
| 153 | + * |
| 154 | + * @return string |
| 155 | + */ |
| 156 | + protected function redirectPath() |
| 157 | + { |
| 158 | + if (method_exists($this, 'redirectTo')) { |
| 159 | + return $this->redirectTo(); |
| 160 | + } |
| 161 | + |
| 162 | + return property_exists($this, 'redirectTo') ? $this->redirectTo : config('admin.route.prefix'); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Send the response after the user was authenticated. |
| 167 | + * |
| 168 | + * @param \Illuminate\Http\Request $request |
| 169 | + * |
| 170 | + * @return \Illuminate\Http\Response |
| 171 | + */ |
| 172 | + protected function sendLoginResponse(Request $request) |
| 173 | + { |
| 174 | + admin_toastr(trans('admin.login_successful')); |
| 175 | + |
| 176 | + $request->session()->regenerate(); |
| 177 | + |
| 178 | + return redirect()->intended($this->redirectPath()); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get the login username to be used by the controller. |
| 183 | + * |
| 184 | + * @return string |
| 185 | + */ |
| 186 | + protected function username() |
| 187 | + { |
| 188 | + return 'username'; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Get the guard to be used during authentication. |
| 193 | + * |
| 194 | + * @return \Illuminate\Contracts\Auth\StatefulGuard |
| 195 | + */ |
| 196 | + protected function guard() |
| 197 | + { |
| 198 | + return Auth::guard('admin'); |
| 199 | + } |
146 | 200 | } |
0 commit comments