xmigas
xmigas

Reputation: 131

Slim HttpBasicAuth

I have single page REST application and I'm trying to add there some really simple authorisation. Just login, no registration. I am using HTTP Basic Authentication middleware with simple PDO authenticator, by Mika Tuupola. This is part of my code:

    require '../vendor/autoload.php';
    require '../src/config.php';

    $app = new Slim\App(['settings' => $config]);


    $container = $app->getContainer();
    $container['db'] = function ($c) {
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'],
        $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};


$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new PdoAuthenticator([
        "pdo" => $pdo
    ])
]));


require '../src/routes.php';

$app->run();

After reloading mypage.dev/roomPictures I just get 500 Internal server error. Do you guys have any idea what I might have been doing wrong?

I'm not sure if I understand this right, but after triggering /roomPictures, the system dialog window asking about username and password should appear?

Thanks in advance!

Upvotes: 1

Views: 631

Answers (1)

Mika Tuupola
Mika Tuupola

Reputation: 20397

Either add the following line to your code:

use Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;

Or alternatively instantiate the middleware as:

$app->add(new Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator([
        "pdo" => $pdo
    ])
]));

Upvotes: 1

Related Questions