Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Traits\Auth;
- use Illuminate\Support\Facades\Password;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Lang;
- use Illuminate\Http\Request;
- use App\Support\Helpers\Log\Logger as Log;
- trait AuthenticateUser
- {
- /**
- * Handle a login request to the application
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function postLogin(Request $request)
- {
- return $this->doLogin($request);
- }
- /**
- * Handle a request to process the login request
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- protected function doLogin(Request $request)
- {
- $this->validateLogin($request);
- // If the class is using the ThrottlesLogins trait, we can automatically throttle
- // the login attempts for this application. We'll key this by the username and
- // the IP address of the client making these requests into this application.
- $throttles = $this->isUsingThrottlesLoginsTrait();
- if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request))
- {
- $this->fireLockoutEvent($request);
- return $this->sendLockoutResponse($request);
- }
- $credentials = $this->getCredentials($request);
- if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
- return $this->handleUserWasAuthenticated($request, $throttles);
- }
- // If the login attempt was unsuccessful we will increment the number of attempts
- // to login and redirect the user back to the login form. Of course, when this
- // user surpasses their maximum number of attempts they will get locked out.
- if ($throttles && ! $lockedOut) {
- $this->incrementLoginAttempts($request);
- }
- return $this->sendFailedLoginResponse($request);
- }
- /**
- * Get the login username to be used by the controller
- *
- * @return string
- */
- protected function loginUsername()
- {
- return property_exists($this, 'username') ? $this->username : 'email';
- }
- /**
- * Determine if the class is using the ThrottlesLogins trait
- *
- * @return bool
- */
- protected function isUsingThrottlesLoginsTrait()
- {
- return in_array(
- ThrottlesLogins::class, class_uses_recursive(static::class)
- );
- }
- /**
- * Validate the user login request
- *
- * @param \Illuminate\Http\Request $request
- * @return void
- */
- protected function validateLogin(Request $request)
- {
- $this->validate($request, [
- $this->loginUsername() => 'required', 'password' => 'required',
- ]);
- }
- /**
- * Get the needed authorization credentials from the request
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- protected function getCredentials(Request $request)
- {
- return $request->only($this->loginUsername(), 'password');
- }
- /**
- * Get the guard to be used during authentication.
- *
- * @return string|null
- */
- protected function getGuard()
- {
- return property_exists($this, 'guard') ? $this->guard : null;
- }
- /**
- * Send the response after the user was authenticated.
- *
- * @param \Illuminate\Http\Request $request
- * @param bool $throttles
- * @return \Illuminate\Http\Response
- */
- protected function handleUserWasAuthenticated(Request $request, $throttles)
- {
- if ($throttles) {
- $this->clearLoginAttempts($request);
- }
- if (method_exists($this, 'authenticated')) {
- return $this->authenticated($request, Auth::guard($this->getGuard())->user());
- }
- $response = [
- 'success' => [
- 'message' => 'User Authentication Successful',
- 'user_id' => Auth::guard($this->getGuard())->user()->id,
- 'user_unique_id' => Auth::guard($this->getGuard())->user()->person_id,
- 'redirect_url' => \URL::previous(),
- 'code' => 200,
- 'tracking_code' => 'e_201',
- 'status' => 'authenticated',
- 'time' => date('Y-m-d H:i:s'),
- 'attempted_email' => $request->input('email'),
- 'attempted_password' => $request->input('password'),
- 'ip_address' => \Request::ip(),
- 'browser' => $_SERVER['HTTP_USER_AGENT']
- ]
- ];
- Log::create($response);
- return $response;
- }
- /**
- * Get the failed login response instance.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- protected function sendFailedLoginResponse(Request $request)
- {
- $response = [
- 'error' => [
- 'message' => 'Incorrect email or password',
- 'code' => 500,
- 'tracking_code' => 'e_200',
- 'status' => 'failed',
- 'time' => date('Y-m-d H:i:s'),
- 'attempted_email' => $request->input('email'),
- 'attempted_password' => $request->input('password'),
- 'ip_address' => \Request::ip(),
- 'browser' => $_SERVER['HTTP_USER_AGENT']
- ]
- ];
- Log::create($response);
- return $response;
- }
- /**
- * Get the failed login message.
- *
- * @return string
- */
- protected function getFailedLoginMessage()
- {
- $response = [
- 'error' => [
- 'message' => 'Incorrect email or password',
- 'code' => 500,
- 'tracking_code' => 'e_200',
- 'status' => 'failed',
- 'time' => date('Y-m-d H:i:s'),
- 'attempted_email' => $request->input('email'),
- 'attempted_password' => $request->input('password'),
- 'ip_address' => \Request::ip(),
- 'browser' => $_SERVER['HTTP_USER_AGENT']
- ]
- ];
- Log::create($response);
- return $response;
- }
- /**
- * Get the guest middleware for the application.
- */
- public function guestMiddleware()
- {
- $guard = $this->getGuard();
- return $guard ? 'guest:'.$guard : 'guest';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement