tuupola/slim-basic-auth

Class 'Tuupola\Middleware\HttpBasicAuthentication' not found

aliahmedisback opened this issue · 32 comments

I am using tuupola with slim framework to secure android connection but I always get this error although I followed the steps that I found in this site :
https://github.com/tuupola/slim-basic-auth

You have either not installed the middleware, not including it in the PHP code or you have installed an old 2.x version of the middleware. 2.x version has slightly different instructions.

my php version is : 7.3.7
I opened my terminal in the project folder and paste this code :
composer require tuupola/slim-basic-auth
then I used this in my own project :
$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
"secure"=>false,
"users" => [
"MyUserName" => "MyUserPassword" ]
]));
that is all what I did.
Did I do anything wrong ??

Are you requiring the autoload.php in the beginning of code? Something like:

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

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

Then I don't know. My guess is that the CLI PHP is 5.x which causes composer to install old version of the middleware. What is the output of:

$ composer require tuupola/slim-basic-auth

?

it was installed successfully.
I have watched a video tutorial about your library and followed the instructions step by step
this is the video :
https://www.youtube.com/watch?v=HsVptQbgav0

I understand. What is the output of above command? Copy and paste it it here. Should look something like:

$ composer require tuupola/slim-basic-auth

Using version ^3.2 for tuupola/slim-basic-auth
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

I put this :
composer require tuupola/slim-basic-auth
and the output was this :
Using version ^3.2 for tuupola/slim-basic-auth
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

That looks correct. I have no idea what causes the error. I would check the filename and line in the error message to be sure where the error is actually thrown. Delete and rewrite the line in case there are some accidental hidden control characters.


Fatal error: Uncaught Error: Class 'Tuupola\Middleware\HttpBasicAuthentication' not found in D:\xampp\htdocs\database\public\index.php:23 Stack trace: #0 {main} thrown in D:\xampp\htdocs\database\public\index.php on line 23
..................... Line 23 is : $app->add(new Tuupola\Middleware\HttpBasicAuthentication([ "secure"=>false, "users" => [ "aliAhmedAbouAli12#" => "thisIsMyPass1993As#" ] ]));

I noticed that the HttpBasicAuthentication.php file is in the path :
\vendor\tuupola\slim-basic-auth
and the HttpBasicAuthentication folder is in path :
\vendor\tuupola\slim-basic-auth\src
does it make any sense?

Yes, composer installs libraries into vendor directory and autoload.php loads them.

what should I do ?

Assuming you are running composer require tuupola/slim-basic-auth in correct folder and correct autoload.php is included, I have no idea what is causing the error.

you mean the path is correct?

Now what ?? what should I do for this error?

I don't know. The error means either middleware is not installed, old version of middleware is installed or middleware is not required in the PHP code. You said all of these have been done correctly. However one of them must be wrong. My guess is composer require was ran in a wrong folder or something similar.

where should I run it?

tuupola path is :
htdocs\database\vendor\tuupola\slim-basic-auth\src
this is my index header :
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\UploadedFileInterface;

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

You should run it in folder where your projects composer.json file is located. It It is the same folder where your vendor/ folder is located. Judging from the code snippet above it is two folders down from the folder where your index.php is located.

For composer usage please refer to composer documentation:
https://getcomposer.org/doc/01-basic-usage.md

it is in the folder you pointed to.... now what ?

I don't know. The problem is not in the middleware.

I am installing the Xampp in #D disk, is that a problem?

this is from composer.json
//////////////////////////////
"require": {
"php": ">=7.1",
"ext-json": "*",
"monolog/monolog": "^1.24",
"php-di/php-di": "^6.0",
"slim/psr7": "^0.5",
"slim/slim": "^4.1",
"tuupola/slim-basic-auth": "^3.2"
},

Maybe try adding a \ in front of the namespace (i.e.):

$app->add(new \Tuupola\Middleware\HttpBasicAuthentication([
"secure"=>false,
"users" => [
"MyUserName" => "MyUserPassword" ]
]));

Dear @tuupola @davehibshman ,
try reproducing this bug or error with Slim Framework 4.x versions.
Since you're using 3.x version that's why you are not able to understand the problem @aliahmed119933 and me are facing.

@aliahmed119933 -- if you're still stuck with this problem then just remove these lines from index.php

$app->add(new \Tuupola\Middleware\HttpBasicAuthentication([
"secure"=>false,
"users" => [
"MyUserName" => "MyUserPassword" ]
]));

You don't need it there. Just go with the Android App's (client) side only.

@tuupola @davehibshman -- this is the solution so you can close this.

@TheCodingArcher are you sure I do not need these lines in the index?
Will the android client understand the whole process ?
Did you test it?

@aliahmed119933 yes I have tested. All the APIs are working perfectly fine.
Just remember, make sure you are using Slim Framework 4.x and above.
For me, its the latest one Slim 4.5.0 released.

Try with my suggestions for now. If something's still not working for you then we can find out some other solution for you. But for me, it's working like a charm. Hope the same for you too.

Cheers!! :)

@TheCodingArcher hey man, I did not tested it yet but I was rude and did not thank you, so thanks man, I appreciate. :-D

@TheCodingArcher
I was using slim framework to write restful api and now with version 4 I feel like I don't know anything about slim....
can you help with transforming from 2 to 4 please? if you have time?

nob96 commented

@aliahmed119933 im using 4.5.0 aswell. After "composer dump-autoload -o" it worked.