Advertisement
Guest User

zz

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