LoginController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  38. * Get the login username to be used by the controller.
  39. *
  40. * @return string
  41. */
  42. public function username()
  43. {
  44. $login = request()->input('email');
  45. $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
  46. request()->merge([$field => $login]);
  47. return $field;
  48. }
  49. public function login(Request $request)
  50. {
  51. $validationRules = [
  52. $this->username() => 'required|string',
  53. 'password' => 'required|string',
  54. ];
  55. if (config('SETTINGS::RECAPTCHA:ENABLED') == 'true') {
  56. $validationRules['g-recaptcha-response'] = ['required', 'recaptcha'];
  57. }
  58. $request->validate($validationRules);
  59. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  60. // the login attempts for this application. We'll key this by the username and
  61. // the IP address of the client making these requests into this application.
  62. if (
  63. method_exists($this, 'hasTooManyLoginAttempts') &&
  64. $this->hasTooManyLoginAttempts($request)
  65. ) {
  66. $this->fireLockoutEvent($request);
  67. return $this->sendLockoutResponse($request);
  68. }
  69. if ($this->attemptLogin($request)) {
  70. $user = Auth::user();
  71. $user->last_seen = now();
  72. $user->save();
  73. return $this->sendLoginResponse($request);
  74. }
  75. // If the login attempt was unsuccessful we will increment the number of attempts
  76. // to login and redirect the user back to the login form. Of course, when this
  77. // user surpasses their maximum number of attempts they will get locked out.
  78. $this->incrementLoginAttempts($request);
  79. return $this->sendFailedLoginResponse($request);
  80. }
  81. }