LoginController.php 2.6 KB

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