Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Illuminate\Foundation\Auth;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Validation\ValidationException;
  8.  
  9. trait AuthenticatesUsers
  10. {
  11. use RedirectsUsers, ThrottlesLogins;
  12.  
  13. /**
  14. * Show the application's login form.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function showLoginForm()
  19. {
  20. return view('auth.login');
  21. }
  22.  
  23. /**
  24. * Handle a login request to the application.
  25. *
  26. * @param \Illuminate\Http\Request $request
  27. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  28. *
  29. * @throws \Illuminate\Validation\ValidationException
  30. */
  31. public function login(Request $request)
  32. {
  33. $this->validateLogin($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 (
  39. method_exists($this, 'hasTooManyLoginAttempts') &&
  40. $this->hasTooManyLoginAttempts($request)
  41. ) {
  42. $this->fireLockoutEvent($request);
  43.  
  44. return $this->sendLockoutResponse($request);
  45. }
  46.  
  47. if ($this->attemptLogin($request)) {
  48. return $this->sendLoginResponse($request);
  49. }
  50.  
  51. // If the login attempt was unsuccessful we will increment the number of attempts
  52. // to login and redirect the user back to the login form. Of course, when this
  53. // user surpasses their maximum number of attempts they will get locked out.
  54. $this->incrementLoginAttempts($request);
  55.  
  56. return $this->sendFailedLoginResponse($request);
  57. }
  58.  
  59. /**
  60. * Validate the user login request.
  61. *
  62. * @param \Illuminate\Http\Request $request
  63. * @return void
  64. *
  65. * @throws \Illuminate\Validation\ValidationException
  66. */
  67. protected function validateLogin(Request $request)
  68. {
  69. $request->validate([
  70. $this->username() => 'required|string',
  71. 'password' => 'required|string',
  72. ]);
  73. }
  74.  
  75. /**
  76. * Attempt to log the user into the application.
  77. *
  78. * @param \Illuminate\Http\Request $request
  79. * @return bool
  80. */
  81. protected function attemptLogin(Request $request)
  82. {
  83. return $this->guard()->attempt(
  84. $this->credentials($request),
  85. $request->filled('remember')
  86. );
  87. }
  88.  
  89. /**
  90. * Get the needed authorization credentials from the request.
  91. *
  92. * @param \Illuminate\Http\Request $request
  93. * @return array
  94. */
  95. protected function credentials(Request $request)
  96. {
  97. return $request->only($this->username(), 'password');
  98. }
  99.  
  100. /**
  101. * Send the response after the user was authenticated.
  102. *
  103. * @param \Illuminate\Http\Request $request
  104. * @return \Illuminate\Http\Response
  105. */
  106. protected function sendLoginResponse(Request $request)
  107. {
  108. $request->session()->regenerate();
  109.  
  110. $this->clearLoginAttempts($request);
  111.  
  112. return $this->authenticated($request, $this->guard()->user())
  113. ?: redirect()->intended($this->redirectPath());
  114. }
  115.  
  116. /**
  117. * The user has been authenticated.
  118. *
  119. * @param \Illuminate\Http\Request $request
  120. * @param mixed $user
  121. * @return mixed
  122. */
  123. protected function authenticated(Request $request, $user)
  124. {
  125. //
  126. }
  127.  
  128. /**
  129. * Get the failed login response instance.
  130. *
  131. * @param \Illuminate\Http\Request $request
  132. * @return \Symfony\Component\HttpFoundation\Response
  133. *
  134. * @throws \Illuminate\Validation\ValidationException
  135. */
  136. protected function sendFailedLoginResponse(Request $request)
  137. {
  138. throw ValidationException::withMessages([
  139. $this->username() => [trans('auth.failed')],
  140. ]);
  141. }
  142.  
  143. /**
  144. * Get the login username to be used by the controller.
  145. *
  146. * @return string
  147. */
  148. public function username()
  149. {
  150. return 'email';
  151. }
  152.  
  153. /**
  154. * Log the user out of the application.
  155. *
  156. * @param \Illuminate\Http\Request $request
  157. * @return \Illuminate\Http\Response
  158. */
  159. public function logout(Request $request)
  160. {
  161. $this->guard()->logout();
  162.  
  163. $request->session()->invalidate();
  164.  
  165. $request->session()->regenerateToken();
  166.  
  167. return $this->loggedOut($request) ?: redirect('/login');
  168. }
  169.  
  170. /**
  171. * The user has logged out of the application.
  172. *
  173. * @param \Illuminate\Http\Request $request
  174. * @return mixed
  175. */
  176. protected function loggedOut(Request $request)
  177. {
  178. //
  179. }
  180.  
  181. /**
  182. * Get the guard to be used during authentication.
  183. *
  184. * @return \Illuminate\Contracts\Auth\StatefulGuard
  185. */
  186. protected function guard()
  187. {
  188. return Auth::guard();
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement