LoginController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Providers\RouteServiceProvider;
  6. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class LoginController extends Controller
  10. {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Login Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. | This controller handles authenticating users for the application and
  17. | redirecting them to your home screen. The controller uses a trait
  18. | to conveniently provide its functionality to your applications.
  19. |
  20. */
  21. use AuthenticatesUsers;
  22. /**
  23. * Where to redirect users after login.
  24. *
  25. * @var string
  26. */
  27. protected $redirectTo = RouteServiceProvider::HOME;
  28. /**
  29. * Create a new controller instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $this->middleware('guest')->except('logout');
  36. }
  37. public function login(Request $request)
  38. {
  39. $validationRules = [
  40. $this->username() => 'required|string',
  41. 'password' => 'required|string',
  42. ];
  43. if (config('SETTINGS::RECAPTCHA:ENABLED') == 'true') {
  44. $validationRules['g-recaptcha-response'] = ['required', 'recaptcha'];
  45. }
  46. $request->validate($validationRules);
  47. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  48. // the login attempts for this application. We'll key this by the username and
  49. // the IP address of the client making these requests into this application.
  50. if (
  51. method_exists($this, 'hasTooManyLoginAttempts') &&
  52. $this->hasTooManyLoginAttempts($request)
  53. ) {
  54. $this->fireLockoutEvent($request);
  55. return $this->sendLockoutResponse($request);
  56. }
  57. if ($this->attemptLogin($request)) {
  58. $user = Auth::user();
  59. $user->last_seen = now();
  60. $user->save();
  61. return $this->sendLoginResponse($request);
  62. }
  63. // If the login attempt was unsuccessful we will increment the number of attempts
  64. // to login and redirect the user back to the login form. Of course, when this
  65. // user surpasses their maximum number of attempts they will get locked out.
  66. $this->incrementLoginAttempts($request);
  67. return $this->sendFailedLoginResponse($request);
  68. }
  69. }