LoginController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\LoginRequest;
  5. use Carbon\Carbon;
  6. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Lang;
  11. use Illuminate\Support\Facades\Log;
  12. class LoginController extends Controller
  13. {
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Login Controller
  17. |--------------------------------------------------------------------------
  18. |
  19. | This controller handles authenticating users for the application.
  20. | The controller uses a trait to conveniently provide its functionality
  21. | to your applications.
  22. |
  23. */
  24. use AuthenticatesUsers;
  25. /**
  26. * The login throttle.
  27. *
  28. * @var int
  29. */
  30. protected $maxAttempts;
  31. /**
  32. * Handle a login request to the application.
  33. *
  34. * @return \Illuminate\Http\JsonResponse
  35. *
  36. * @throws \Illuminate\Validation\ValidationException
  37. */
  38. public function login(LoginRequest $request)
  39. {
  40. Log::info(sprintf('User login requested by %s from %s', var_export($request['email'], true), $request->ip()));
  41. $this->maxAttempts = config('auth.throttle.login');
  42. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  43. // the login attempts for this application. We'll key this by the username and
  44. // the IP address of the client making these requests into this application.
  45. if (method_exists($this, 'hasTooManyLoginAttempts') &&
  46. $this->hasTooManyLoginAttempts($request)) {
  47. $this->fireLockoutEvent($request);
  48. Log::notice(sprintf(
  49. '%s from %s locked-out, too many failed login attempts (using email+password)',
  50. var_export($request['email'], true),
  51. $request->ip()
  52. ));
  53. return $this->sendLockoutResponse($request);
  54. }
  55. if ($this->attemptLogin($request)) {
  56. return $this->sendLoginResponse($request);
  57. }
  58. // If the login attempt was unsuccessful we will increment the number of attempts
  59. // to login and redirect the user back to the login form. Of course, when this
  60. // user surpasses their maximum number of attempts they will get locked out.
  61. $this->incrementLoginAttempts($request);
  62. Log::notice(sprintf(
  63. 'Failed login for %s from %s - Attemp %d/%d (using email+password)',
  64. var_export($request['email'], true),
  65. $request->ip(),
  66. $this->limiter()->attempts($this->throttleKey($request)),
  67. $this->maxAttempts()
  68. ));
  69. return $this->sendFailedLoginResponse($request);
  70. }
  71. /**
  72. * log out current user
  73. *
  74. * @return \Illuminate\Http\JsonResponse
  75. */
  76. public function logout(Request $request)
  77. {
  78. $user = $request->user();
  79. Auth::logout();
  80. Log::info(sprintf('User ID #%s logged out', $user->id));
  81. return response()->json(['message' => 'signed out'], Response::HTTP_OK);
  82. }
  83. /**
  84. * Send the response after the user was authenticated.
  85. *
  86. * @return \Illuminate\Http\JsonResponse
  87. */
  88. protected function sendLoginResponse(Request $request)
  89. {
  90. $this->clearLoginAttempts($request);
  91. $name = $this->guard()->user()?->name;
  92. $this->authenticated($request, $this->guard()->user());
  93. return response()->json([
  94. 'message' => 'authenticated',
  95. 'name' => $name,
  96. 'preferences' => $this->guard()->user()->preferences,
  97. 'is_admin' => $this->guard()->user()->is_admin,
  98. ], Response::HTTP_OK);
  99. }
  100. /**
  101. * Get the failed login response instance.
  102. *
  103. * @return \Illuminate\Http\JsonResponse
  104. */
  105. protected function sendFailedLoginResponse(Request $request)
  106. {
  107. return response()->json(['message' => 'unauthorized'], Response::HTTP_UNAUTHORIZED);
  108. }
  109. /**
  110. * Redirect the user after determining they are locked out.
  111. *
  112. * @return \Illuminate\Http\JsonResponse
  113. */
  114. protected function sendLockoutResponse(Request $request)
  115. {
  116. $seconds = $this->limiter()->availableIn(
  117. $this->throttleKey($request)
  118. );
  119. return response()->json(['message' => Lang::get('auth.throttle', ['seconds' => $seconds])], Response::HTTP_TOO_MANY_REQUESTS);
  120. }
  121. /**
  122. * Get the needed authorization credentials from the request.
  123. *
  124. * @return array
  125. */
  126. protected function credentials(Request $request)
  127. {
  128. $credentials = [
  129. $this->username() => strtolower($request->input($this->username())),
  130. 'password' => $request->get('password'),
  131. ];
  132. return $credentials;
  133. }
  134. /**
  135. * The user has been authenticated.
  136. *
  137. * @param mixed $user
  138. * @return void
  139. */
  140. protected function authenticated(Request $request, $user)
  141. {
  142. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  143. $user->save();
  144. Log::info(sprintf('User ID #%s authenticated (using email+password)', $user->id));
  145. }
  146. }