Advertisement
Guest User

Authentication

a guest
Oct 26th, 2016
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.97 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Traits\Auth;
  4.  
  5. use Illuminate\Support\Facades\Password;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Lang;
  8. use Illuminate\Http\Request;
  9.  
  10. use App\Support\Helpers\Log\Logger as Log;
  11.  
  12. trait AuthenticateUser
  13. {
  14.     /**
  15.      * Handle a login request to the application
  16.      *
  17.      * @param  \Illuminate\Http\Request  $request
  18.      * @return \Illuminate\Http\Response
  19.      */
  20.     public function postLogin(Request $request)
  21.     {
  22.         return $this->doLogin($request);
  23.     }
  24.  
  25.     /**
  26.      * Handle a request to process the login request
  27.      *
  28.      * @param  \Illuminate\Http\Request  $request
  29.      * @return \Illuminate\Http\Response
  30.      */
  31.     protected function doLogin(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.         $throttles = $this->isUsingThrottlesLoginsTrait();
  39.  
  40.         if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request))
  41.         {
  42.             $this->fireLockoutEvent($request);
  43.             return $this->sendLockoutResponse($request);
  44.         }
  45.  
  46.         $credentials = $this->getCredentials($request);
  47.  
  48.         if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
  49.             return $this->handleUserWasAuthenticated($request, $throttles);
  50.         }
  51.  
  52.         // If the login attempt was unsuccessful we will increment the number of attempts
  53.         // to login and redirect the user back to the login form. Of course, when this
  54.         // user surpasses their maximum number of attempts they will get locked out.
  55.         if ($throttles && ! $lockedOut) {
  56.             $this->incrementLoginAttempts($request);
  57.         }
  58.  
  59.         return $this->sendFailedLoginResponse($request);
  60.     }
  61.    
  62.     /**
  63.      * Get the login username to be used by the controller
  64.      *
  65.      * @return string
  66.      */
  67.     protected function loginUsername()
  68.     {
  69.         return property_exists($this, 'username') ? $this->username : 'email';
  70.     }
  71.  
  72.     /**
  73.      * Determine if the class is using the ThrottlesLogins trait
  74.      *
  75.      * @return bool
  76.      */
  77.     protected function isUsingThrottlesLoginsTrait()
  78.     {
  79.         return in_array(
  80.             ThrottlesLogins::class, class_uses_recursive(static::class)
  81.         );
  82.     }
  83.  
  84.     /**
  85.      * Validate the user login request
  86.      *
  87.      * @param  \Illuminate\Http\Request  $request
  88.      * @return void
  89.      */
  90.     protected function validateLogin(Request $request)
  91.     {
  92.         $this->validate($request, [
  93.             $this->loginUsername() => 'required', 'password' => 'required',
  94.         ]);
  95.     }
  96.  
  97.     /**
  98.      * Get the needed authorization credentials from the request
  99.      *
  100.      * @param  \Illuminate\Http\Request  $request
  101.      * @return array
  102.      */
  103.     protected function getCredentials(Request $request)
  104.     {
  105.         return $request->only($this->loginUsername(), 'password');
  106.     }
  107.  
  108.     /**
  109.      * Get the guard to be used during authentication.
  110.      *
  111.      * @return string|null
  112.      */
  113.     protected function getGuard()
  114.     {
  115.         return property_exists($this, 'guard') ? $this->guard : null;
  116.     }
  117.  
  118.     /**
  119.      * Send the response after the user was authenticated.
  120.      *
  121.      * @param  \Illuminate\Http\Request  $request
  122.      * @param  bool  $throttles
  123.      * @return \Illuminate\Http\Response
  124.      */
  125.     protected function handleUserWasAuthenticated(Request $request, $throttles)
  126.     {
  127.         if ($throttles) {
  128.             $this->clearLoginAttempts($request);
  129.         }
  130.  
  131.         if (method_exists($this, 'authenticated')) {
  132.             return $this->authenticated($request, Auth::guard($this->getGuard())->user());
  133.         }
  134.  
  135.         $response = [
  136.             'success' => [
  137.                 'message' => 'User Authentication Successful',
  138.                 'user_id' => Auth::guard($this->getGuard())->user()->id,
  139.                 'user_unique_id' => Auth::guard($this->getGuard())->user()->person_id,
  140.                 'redirect_url' => \URL::previous(),
  141.                 'code' => 200,
  142.                 'tracking_code' => 'e_201',
  143.                 'status' => 'authenticated',
  144.                 'time' => date('Y-m-d H:i:s'),
  145.                 'attempted_email' => $request->input('email'),
  146.                 'attempted_password' => $request->input('password'),
  147.                 'ip_address' => \Request::ip(),
  148.                 'browser' => $_SERVER['HTTP_USER_AGENT']
  149.             ]
  150.         ];
  151.  
  152.         Log::create($response);
  153.         return $response;
  154.     }
  155.  
  156.     /**
  157.      * Get the failed login response instance.
  158.      *
  159.      * @param \Illuminate\Http\Request  $request
  160.      * @return \Illuminate\Http\Response
  161.      */
  162.     protected function sendFailedLoginResponse(Request $request)
  163.     {
  164.         $response = [
  165.             'error' => [
  166.                 'message' => 'Incorrect email or password',
  167.                 'code' => 500,
  168.                 'tracking_code' => 'e_200',
  169.                 'status' => 'failed',
  170.                 'time' => date('Y-m-d H:i:s'),
  171.                 'attempted_email' => $request->input('email'),
  172.                 'attempted_password' => $request->input('password'),
  173.                 'ip_address' => \Request::ip(),
  174.                 'browser' => $_SERVER['HTTP_USER_AGENT']
  175.             ]
  176.         ];
  177.  
  178.         Log::create($response);
  179.         return $response;
  180.     }
  181.  
  182.     /**
  183.      * Get the failed login message.
  184.      *
  185.      * @return string
  186.      */
  187.     protected function getFailedLoginMessage()
  188.     {
  189.         $response = [
  190.             'error' => [
  191.                 'message' => 'Incorrect email or password',
  192.                 'code' => 500,
  193.                 'tracking_code' => 'e_200',
  194.                 'status' => 'failed',
  195.                 'time' => date('Y-m-d H:i:s'),
  196.                 'attempted_email' => $request->input('email'),
  197.                 'attempted_password' => $request->input('password'),
  198.                 'ip_address' => \Request::ip(),
  199.                 'browser' => $_SERVER['HTTP_USER_AGENT']
  200.             ]
  201.         ];
  202.  
  203.         Log::create($response);
  204.         return $response;
  205.     }
  206.  
  207.     /**
  208.      * Get the guest middleware for the application.
  209.      */
  210.     public function guestMiddleware()
  211.     {
  212.         $guard = $this->getGuard();
  213.  
  214.         return $guard ? 'guest:'.$guard : 'guest';
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement