LoginController.php 4.4 KB

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