LoginController.php 2.8 KB

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