A.Ali
A.Ali

Reputation: 13

How to use basic authentication in slim php?

I am using slim framework to write my own API and everything is just fine but when I add the slim-basic-auth library to secure my API and test it .... I always get this error :

Uncaught Error: Class 'Tuupola\Middleware\HttpBasicAuthentication' not found in D:\xampp\htdocs\mydatabase\public\index.php:22

Any help with that please ?

I installed the library and use it just like in tutorial

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
        "secure"=>false,
        "users" => [
                "userName@#" => "password#@"        ]
        ]));

Upvotes: 0

Views: 2593

Answers (2)

Pramaganus
Pramaganus

Reputation: 141

For anyone dealing with this issue: In addition to registering the middleware in your Middlewares.php (or where your $app = new Slim\App; is defined):

return static function (App $app, Closure $customErrorHandler): void {
    <...>
    $app->add(new \Tuupola\Middleware\HttpBasicAuthentication([
        "users" => [
            "somebody" => "passw0rd"
        ]
    ]));
};

If you're using fast CGI you must add this line to your public/.htaccess file to avoid getting 401 even with valid auth:

RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Source: https://bugs.php.net/bug.php?id=67883

Upvotes: 1

jDolba
jDolba

Reputation: 411

you are missing backslash

new Tuupola\Middleware\HttpBasicAuthentication

=>

new \Tuupola\Middleware\HttpBasicAuthentication

Upvotes: 0

Related Questions