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