Slim 3/4 Authentication Middleware

Hi All,

Request your help, the below code does not work for some reason, we have tried in Slim 3, Slim 4 with both Slim PSR-7 and Nyholm PSR-7 and Nyholm PSR-7 Server with each of the below options, it does not throw any errors while accessing the URL from fire fox where as sometime it throws HTTP NOT found while using chrome browser, the required base path setting and .htaccess are been configured as specified in the url “https://github.com/selective-php/basepath#slim-4-integration

Note: This issue occurs only for the routes which we added to be authenticated(‘login’,‘require’,‘f401’), rest of the routes are working fine

Options

1. $response = $handler->handle($request);
2. $response = new Response(200);   - Nyholm
3. $responseFactory = new \Nyholm\Psr7\Factory\Psr17Factory();   - Nyholm
    $response = $responseFactory->createResponse(200);  - Nyholm
4.  $response = new \Slim\Psr7\Response();  - Slim
5.  $responseFactory = new \Slim\Psr7\Factory\ResponseFactory(); - Slim
     response = $responseFactory->createResponse(200); - Slim

Code

<?php
use Slim\Routing\RouteContext;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Exception\NotFoundException;
#use Nyholm\Psr7\Factory\Psr17Factory;
#use Nyholm\Psr7\Response;

class authorization {
      public function __invoke($request, $handler): Response {
                      $routeContext = RouteContext::fromRequest($request);
                      $route = $routeContext->getRoute();
                      if(empty($route)) { throw new NotFoundException($request, $response); }
                      $routeName = $route->getName();
                      $groups = $route->getGroups();
                      $publicRoutesArray = array('login','require','f401');
                      if(empty($_SESSION['user']) && in_array($routeName, $publicRoutesArray)) {
                         $routeParser = RouteContext::fromRequest($request)->getRouteParser();
                         $url = $routeParser->urlFor('login');
                         #$response = $handler->handle($request);
                         #$response = new \Slim\Psr7\Response();
                         #$response = new Response(200);
                         #$responseFactory = new \Nyholm\Psr7\Factory\Psr17Factory();
                         #$response = $responseFactory->createResponse(200);
                         $responseFactory = new \Slim\Psr7\Factory\ResponseFactory();
                         $response = $responseFactory->createResponse(200);
                         return $response->withHeader('Location', 'login')->withStatus(301);
                      } else { $response = $handler->handle($request); }
             return $response;
      }
}

?>

middleware.php

<?php

use Slim\App;
use Slim\Views\TwigMiddleware;
use Selective\Config\Configuration;
use Selective\BasePath\BasePathMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\ContentLengthMiddleware;

(require __DIR__ . '/authorization.php');

return function (App $app) {
       $routingMiddleware = new Slim\Middleware\RoutingMiddleware(
                $app->getRouteResolver(),
                $app->getRouteCollector()->getRouteParser()
       );
       $app->add(TwigMiddleware::createFromContainer($app));
       $app->addBodyParsingMiddleware();
       $contentLengthMiddleware = new ContentLengthMiddleware();
       $app->add($contentLengthMiddleware);
       $authorization = new authorization();
       $app->add($authorization);
       #$app->addMiddleware($routingMiddleware);
       $app->addRoutingMiddleware();
       $app->add(BasePathMiddleware::class);
       $app->add(ErrorMiddleware::class);
};
?>

container.php

<?php

use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\Twig;
use Selective\Config\Configuration;
use Selective\BasePath\BasePathMiddleware;

return [
    Configuration::class => function () {
        return new Configuration(require __DIR__ . '/settings.php');
    },

    App::class => function (ContainerInterface $container) {
        AppFactory::setContainer($container);
        $container->set('view', function() { return Twig::create('../tpl', ['cache' => false]); });
        $app = AppFactory::create();
        return $app;
    },

    ErrorMiddleware::class => function (ContainerInterface $container) {
        $app = $container->get(App::class);
        $settings = $container->get(Configuration::class)->getArray('error_handler_middleware');

        return new ErrorMiddleware(
            $app->getCallableResolver(),
            $app->getResponseFactory(),
            (bool)$settings['display_error_details'],
            (bool)$settings['log_errors'],
            (bool)$settings['log_error_details']
        );
    },

    BasePathMiddleware::class => function (ContainerInterface $container) {
        return new BasePathMiddleware($container->get(App::class));
    },
];

?>

Hi All,

Issue resolved, the issue was at apache side