The CORS middleware handle.

@param \Illuminate\Http\Request $request
@param \Closure                 $next

@return mixed

@author Seven Du <shiweidu@outlook.com>
779387
public function handle($request, Closure $next)
    {
        $type = 'laravel';
        if ($this->cors->isPreflightRequest($type, $request)) {
            $this->cors->setRequest($type, $request);
            $this->cors->setResponse($type, $response = new Response());
            $this->cors->handle();

            // Set Preflight response status code.
            $response->setStatusCode(204);

            return $response;
        }

        return $next($request);
    }
1359535
public function handle($request, Closure $next)
    {
        if (Config::get('cms.api-token') == $request->get('token')) {
            return $next($request);
        }

        return response('Unauthorized.', 401);
    }
414210
public function handle($request, \Closure $next)
    {
        $token = $this->authenticate($request);
        $response = $next($request);

        if ($token) {
            $response->header('Authorization', 'Bearer ' . $token);
        }

        return $response;
    }
497494
public function handle(Request $request, Closure $next)
    {

        if (!$this->inPrefixes()) {
            return $next($request);
        }

        if ($this->inExcludes()) {
            return $next($request);
        }

        if (!$this->inDomains()) {
            return $next($request);
        }

        // CORS
        if (config('api.cors.cors', true)) {
            if (isset($_SERVER['HTTP_ORIGIN'])) {
                header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
            } else {
                header('Access-Control-Allow-Origin: *');
            }
            if (config('api.cors.credentials', true)) {
                header('Access-Control-Allow-Credentials: true');
            }
            header('Access-Control-Allow-Headers: Origin, X-Requested-With, No-Cache, Authorization, X-Auth-Token, Content-Type, Accept');
            header('Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS, TRACE');
            $age = config('api.cors.max_age', 86400);
            header("Access-Control-Max-Age: $age");
        }


        try {
            $response = $this->sendRequestThroughRouter($request);
        } catch (Exception $exception) {

            if ($this->isCompatibleWithDingo($exception)) {
                return $next($request);
            }


            $this->exception->report($exception);
            $response = $this->exception->handle($exception);

        }


        return $response;
    }
705378
public function handle($request, Closure $next)
    {
        if (!Sentinel::check()) {
            if ($request->expectsJson()) {
                $message = $this->translate('unauthorized', 'Unauthorized');
                return response()->json(['error' => $message], 401);
            } else {
                return redirect()->guest(route('auth.login.form'));
            }
        }

        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);
    }
873539
public function handle(Request $request, \Closure $next)
	{
		$this->buildRepository($request);

		return $next($request);
	}
987842
public function handle(Request $request, Closure $next)
    {
        $request::setTrustedProxies([], $this->getTrustedHeaderNames()); // Reset trusted proxies between requests
        $this->setTrustedProxyIpAddresses($request);

        return $next($request);
    }
1092459
public function handle($request, Closure $next)
    {
        $locale = $request->segment(1);

        $language = Language::findByCode($locale);

        if ($language) {
            App::setLocale($language->code);
            $request->setDefaultLocale($language->code);
        } else {
            $request->setDefaultLocale(config('app.locale'));
        }

        return $next($request);
    }
1119153
public function handle(Request $request, Closure $next)
    {
        $request = $this->processRequest($request);
        return $next($request);
    }
955224
public function handle(Request $request, Closure $next)
    {
        if ($this->auth->check()) {
            return new RedirectResponse('/');
        }

        return $next($request);
    }
702526
public function handle($request, Closure $next)
    {
        $request->setTrustedProxies([
            $request->getClientIp(),
        ]);

        if (!$request->secure() && app()->environment('production')) {
                return redirect()->secure($request->getRequestUri());
        }

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

        return $next($request);
    }
4140
public function handle($request, Closure $next)
    {
        if (! $this->checkIfUserCanViewPost($request)) {
            return redirect()->route('/');
        }

        return $next($request);
    }
889479
public function handle($request, Closure $next)
    {
        if (Cookie::has('language')) {
            Config::set('app.locale', Cookie::get('language'));
        }

        return $next($request);
    }
611914
public function handle($request, Closure $next)
    {
        $paginationKey = config('pagination.key');

        $request->offsetSet($paginationKey, $request->paginationCount());

        return $next($request);
    }
460009
public function handle($request, Closure $next)
    {
        $type = 'laravel';

        // The request not is CORS request,
        // break the handle.
        if (!$this->hasShouldRouteGroup($request) || !$this->hasAllowRoutePerfix($request) || !$this->cors->isCorsRequest($type, $request)) {
            return $next($request);

        // Check the request is option,
        // is "option" request return CORS headers response.
        } elseif ($this->cors->isPreflightRequest($type, $request)) {
            $this->corsHandle($request, $response = new Response());

            // Set Preflight response status code.
            $response->setStatusCode(204);

            return $response;

        // Add the headers on the Request Handled event as fallback in case of exceptions
        } elseif (class_exists(RequestHandled::class)) {
            $this->events->listen(RequestHandled::class, function (RequestHandled $event) {
                $event->response = $this->corsHandle($event->request, $event->response);
            });
        }

        return $this->corsHandle($request, $next($request));
    }