Handle the authentication middler

@param \Illuminate\Http\Request $request
@param \Closure                 $next
@param null|string              $guard
@return mixed
@throws \League\OAuth2\Server\Exception\InvalidRequestException
13050
public function handle(Request $request, Closure $next, $guard = null)
	{
		if (is_null($guard)) {
			throw new \LogicException(self::class . ' called with no guard defined.');
		}

		// Attempt to resolve the application user, throws \League\OAuth2\Server\Exception\AccessDeniedException
		// if the token is invalid, or \League\OAuth2\Server\Exception\InvalidRequestException if the token
		// is missing
		$user = Auth::guard($guard)->user();

		// If above still resulted in a null user throw an error
		if (is_null($user)) {
			throw new InvalidRequestException('access token');
		}

		return $next($request);
	}
1135625
public function handle($request, Closure $next)
    {
        if (! $this->auth->user()->managesUsers()) {
            return response('Unauthorized.', 401);
        }

        return $next($request);
    }
252307
public function handle( $request, Closure $next, $guard = null )
	{
		if ( Acct::check() )
			return Redirect::to( '/' );

		return $next( $request );
	}
1133354
public function handle(Request $request, Closure $next)
    {
        if ($this->auth->guard('wink')->check()) {
            $this->auth->shouldUse('wink');
        } else {
            throw new AuthenticationException(
                'Unauthenticated.', ['wink'], route('wink.auth.login')
            );
        }

        return $next($request);
    }
603681
public function handle(Request $request, Closure $next)
    {
        if (!$this->verificationService->mustBeVerified($request->route(), $this->guard)) {
            return $next($request);
        }

        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        }

        return redirect()->to($this->verificationService->getPathToVerificationAction());
    }
479451
public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest(route('authentication::session.create'));
            }
        }

        $user = $this->auth->user();

        if ($user->isBanned()) {
            $this->auth->logout();
            return redirect()->route('authentication::session.create')->withErrors([
                'authentication::login_error' => $this->translator->get('authentication::user.banned', ['email' => $user->email])
            ]);
        }

        return $next($request);
    }
1164382
public function handle($request, Closure $next)
    {
        if (! AuthRagnarok::check()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/auth/login');
            }
        }

        return $next($request);
    }
268531
public function handle($request, Closure $next, ...$guards)
    {
        try {
            if(sharp_laravel_version_gte('5.7')) {
                // Laravel's API changed in 5.7
                // @see https://laravel.com/docs/5.7/upgrade
                $this->authenticate($request, $guards);

            } else {
                $this->authenticate($guards);
            }

            if($checkHandler = config('sharp.auth.check_handler')) {
                if(!app($checkHandler)->check(auth()->guard($guards[0] ?? null)->user())) {
                    throw new AuthenticationException();
                }
            }

        } catch (AuthenticationException $e) {
            if($request->wantsJson()) {
                return response()->json(['message' => "Unauthenticated user"], 401);
            }

            return redirect()->guest(
                config('sharp.auth.login_page_url', route("code16.sharp.login"))
            );
        }

        return $next($request);
    }
872332
public function handle($request, Closure $next)
    {
        if (!$request->user()->is('admin')) {
            return redirect()->route('admin.login.index');
        }

        return $next($request);
    }
8927
public function handle($request, Closure $next)
    {
        $this->authenticate($request);

        $response = $next($request);

        // Send the refreshed token back to the client.
        return $this->setAuthenticationHeader($response);
    }
805307
public function handle($request, Closure $next)
    {
        if( auth()->check() ) {
            $user = auth()->user();
            $user->updateLastActivity();
        }

        return $next($request);
    }
345518
public function handle($request, Closure $next, $guard = null)
    {

        if (Request::session()->get('language')) {
            App::setLocale(Request::session()->get('language'));
        } else {
            App::setLocale(Config::get('admin.defaults.language'));
        }

        return $next($request);
    }
305063
public function handle($request, Closure $next, $guard = null) {
		if (admin()->user()) {
			return redirect(aurl('/'));
		}
		return $next($request);

	}
1257827
public function handle(Request $request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            }

            return redirect()->guest(\Flare::adminUrl('login'));
        }

        return $next($request);
    }
285554
public function handle($request, Closure $next)
    {
        $route = $this->router->getCurrentRoute();

        if (! $this->auth->check(false)) {
            $this->auth->authenticate($route->getAuthenticationProviders());
        }

        return $next($request);
    }
200434
public function handle(Request $request, Closure $next, MediaTypeGuard $guard = null)
    {
        //
        $guard = $guard ?? app(MediaTypeGuard::class);

        if (!$guard->validateExistingContentType($request) || !$guard->hasCorrectHeadersForData($request)) {
            $errors = (new ErrorCollection())->add(ErrorFactory::buildUnsupportedMediaType());
            $encoder = Encoder::instance([], new EncoderOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

            return new Response($encoder->encodeErrors($errors), 415, ['Content-Type' => $guard->getContentType()]);
        }

        if (!$guard->hasCorrectlySetAcceptHeader($request)) {
            $errors = (new ErrorCollection())->add(ErrorFactory::buildUnacceptable());
            $encoder = Encoder::instance([], new EncoderOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

            return new Response($encoder->encodeErrors($errors), 406, ['Content-Type' => $guard->getContentType()]);
        }

        $response = $next($request);
        $response->header('Content-Type', $guard->getContentType());

        return $response;
    }
1351242
public function handle(Request $request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            if ($request->expectsJson()) {
                return response()->json(Auth::user());
            }

            return redirect()->route('admin.dashboard');
        }

        return $next($request);
    }