tuupola / slim-basic-auth

PSR-7 and PSR-15 HTTP Basic Authentication Middleware
MIT License
440 stars 66 forks source link

Class 'Tuupola\Middleware\HttpBasicAuthentication' not found #84

Closed aliahmedisback closed 4 years ago

aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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.

aliahmedisback commented 4 years ago

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 ??

tuupola commented 4 years ago

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

require __DIR__ . "/vendor/autoload.php";
aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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

?

aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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
aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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.

aliahmedisback commented 4 years ago


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#" ] ]));

aliahmedisback commented 4 years ago

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?

tuupola commented 4 years ago

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

aliahmedisback commented 4 years ago

what should I do ?

tuupola commented 4 years ago

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.

aliahmedisback commented 4 years ago

you mean the path is correct?

aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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.

aliahmedisback commented 4 years ago

where should I run it?

aliahmedisback commented 4 years ago

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';

tuupola commented 4 years ago

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

aliahmedisback commented 4 years ago

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

tuupola commented 4 years ago

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

aliahmedisback commented 4 years ago

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

aliahmedisback commented 4 years ago

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" },

davehibshman commented 4 years ago

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

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

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.

TheCodingArcher commented 4 years ago

@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.

aliahmedisback commented 4 years ago

@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?

TheCodingArcher commented 4 years ago

@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!! :)

aliahmedisback commented 4 years ago

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

aliahmedisback commented 4 years ago

@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 4 years ago

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