tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.24k stars 1.55k forks source link

Only when testing ReflectionException: Class Tymon\JWTAuth\Middleware\GetUserFromToken #967

Open taos-thiagoaos opened 7 years ago

taos-thiagoaos commented 7 years ago

When i run vendor/bin/phpunit show the error

ReflectionException: Class Tymon\JWTAuth\Middleware\GetUserFromToken

/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Container/Container.php:741
/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Container/Container.php:631
/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:674
/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:142
/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:396
/home/thiagoaos/workspace/projects/php/base/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:71
/home/thiagoaos/workspace/projects/php/base/tests/UserTest.php:25

But the application works like a charm in develop mode.

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'cors' => \App\Http\Middleware\Cors::class,
        'jwt.auth' => "Tymon\JWTAuth\Middleware\GetUserFromToken",
        'jwt.refresh' => "Tymon\JWTAuth\Middleware\RefreshToken",
        'acl.role' => \App\Http\Middleware\HasRole::class,
        'acl.permission' => \App\Http\Middleware\HasPermission::class,
        'develop.auth' => \App\Http\Middleware\DevelopAuth::class
    ];

I test with class approach too

    'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
    'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
    'providers' => [
    ...
        Tymon\JWTAuth\Providers\LaravelServiceProvider::class
    ],
    'aliases' => [
    ...
        'JWTAuth'       => Tymon\JWTAuth\Facades\JWTAuth::class,
        'JWTFactory'    => Tymon\JWTAuth\Facades\JWTFactory::class
    ],

I was using alpha.2 (another Provider config) and now i test with this config using beta.1. Both work in development but in test throgth vendor/bin/phpunit no.

Need some config to work in test?

gmann1982 commented 7 years ago

Getting the same error when using Laravel 5.4

Class Tymon\JWTAuth\Middleware\GetUserFromToken does not exist

gmann1982 commented 7 years ago

Fixed by changing:

Tymon\JWTAuth\Middleware\GetUserFromToken::class

to

Tymon\JWTAuth\Http\Middleware\Authenticate::class

taos-thiagoaos commented 7 years ago

I see that in source code :) The middleware in alpha 2 and beta 1 change the name. And now, don't need more do declare in kernel.php, the new Provider already did that automatically.

The strange thing is that only happened the error when i started the system by phpunit

jacmkno commented 6 years ago

I had the same issue, and changing the name worked :) but for me it was also working fine before the change from outside phpunit. Does this mean that the library is loaded twice (with the old name and the new name) for calls outside phpunit? This worries me a little. Can someone help us find out why it was working from outside phpunit with the old name?

BTW. With the new name it works fine form curl and from phpunit so this is quite weird...

gileneusz commented 6 years ago

@gmann1982 now it gives: Class Tymon\\JWTAuth\\Middleware\\Authenticate does not exist LOL, I hate it

ahmadnwira commented 5 years ago

this worked for me using [laravel 5.6 & jwt-auth 1.0]

'jwt.auth' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
'jwt.refresh' => \TymonJWTAuth\MiddlewareRefreshToken::class
tiaguinhor commented 5 years ago

laravel 5.6+ & jwt-auth 1.0+

'jwt.auth' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
'jwt.refresh' => \Tymon\JWTAuth\Http\Middleware\RefreshToken::class,
purplenimbus commented 3 years ago

laravel 5.6+ & jwt-auth 1.0+

'jwt.auth' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
'jwt.refresh' => \Tymon\JWTAuth\Http\Middleware\RefreshToken::class,

This worked for me

"laravel/framework": "^7.0" "tymon/jwt-auth": "dev-develop"