Skip to content

Basic-Auth ​

This middleware implements HTTP Basic Authentication. It was originally developed for Slim but can be used with all frameworks using PSR-7 or PSR-15 style middlewares. It has been tested with Slim Framework and Zend Expressive.

INFO

This package is a maintained version of a tuupola/slim-basic-auth, version v1.x is forked from 3.x and will remain 100% backward compatible with the original library.

php
use Slim\Factory\AppFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Tuupola\Middleware\HttpBasicAuthentication;

require __DIR__ . '/vendor/autoload.php';

$app = AppFactory::create();
$app->addErrorMiddleware(true, true, true);
$app->addMiddleware(new HttpBasicAuthentication([
    'users' => ['cosmo' => 'tooManySecrets'],
]));

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello, ' . $request->getServerParams()['PHP_AUTH_USER']);

    return $response;
});

$app->run();