LoginController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 \Illuminate\Http\Request $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. * Send the response after the user was authenticated.
  54. *
  55. * @param \Illuminate\Http\Request $request
  56. * @return \Illuminate\Http\JsonResponse
  57. */
  58. protected function sendLoginResponse(Request $request)
  59. {
  60. $this->clearLoginAttempts($request);
  61. $success['token'] = $this->guard()->user()->createToken('2FAuth')->accessToken;
  62. $success['name'] = $this->guard()->user()->name;
  63. $this->authenticated($request, $this->guard()->user());
  64. return response()->json([
  65. 'message' => 'authenticated',
  66. 'token' => $success['token'],
  67. 'name' => $success['name']
  68. ], Response::HTTP_OK);
  69. }
  70. /**
  71. * Get the failed login response instance.
  72. *
  73. * @param \Illuminate\Http\Request $request
  74. * @return \Illuminate\Http\JsonResponse
  75. */
  76. protected function sendFailedLoginResponse(Request $request)
  77. {
  78. return response()->json(['message' => 'unauthorised'], Response::HTTP_UNAUTHORIZED);
  79. }
  80. /**
  81. * Redirect the user after determining they are locked out.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @return \Illuminate\Http\JsonResponse
  85. */
  86. protected function sendLockoutResponse(Request $request)
  87. {
  88. $seconds = $this->limiter()->availableIn(
  89. $this->throttleKey($request)
  90. );
  91. return response()->json(['message' => Lang::get('auth.throttle', ['seconds' => $seconds])], Response::HTTP_TOO_MANY_REQUESTS);
  92. }
  93. /**
  94. * Get the needed authorization credentials from the request.
  95. *
  96. * @param \Illuminate\Http\Request $request
  97. * @return array
  98. */
  99. protected function credentials(Request $request)
  100. {
  101. $credentials = [
  102. $this->username() => strtolower($request->input($this->username())),
  103. 'password' => $request->get('password'),
  104. ];
  105. return $credentials;
  106. }
  107. /**
  108. * The user has been authenticated.
  109. *
  110. * @param \Illuminate\Http\Request $request
  111. * @param mixed $user
  112. * @return mixed
  113. */
  114. protected function authenticated(Request $request, $user)
  115. {
  116. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  117. $user->save();
  118. }
  119. }