tymondesigns / jwt-auth

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

Class Tymon\JWTAuth\Middleware\GetUserFromToken does not exis #2009

Open mr-Sepi0l opened 4 years ago

mr-Sepi0l commented 4 years ago

Class Tymon\JWTAuth\Middleware\GetUserFromToken does not exists

I'm trying to run tests in laravel with JWT-AUTH. I've been able to run test for the login, logout and refresh and everything works as intended. But as soon as I wanna use an endpoint behind jwt-auth middleware I got this error. I've tried several solution I've found on stack overflow but none of them fixes the issue. As far as I know, no matter how I setup my call in my test, I always got this error.

I already tried to composer dump-autoload and it doesn't fix the issue.

Your environment

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 7.20
Package version 1.0
PHP version 7.4

Steps to reproduce

Just make a call behind jwt-auth middleware

api.php

Route::group(['middleware' => ['jwt.auth', /*'jwt.refresh',*/ 'api']], function () {
    Route::post('users/{user}/avatar', UploadController::class.'@avatar');

    Route::resource('users', UsersController::class, ['except' => ['edit', 'create']]);
    Route::resource('messages', MessagesController::class, ['except' => ['edit', 'create']]);
    Route::resource('companies', CompaniesController::class, ['except' => ['edit', 'create']]);
});

my test

/** @test */
    public function a_message_requires_a_content()
    {
        $this->signIn();

        $this->json('post', '/messages', [])
            ->dump();
    }

 protected function signIn(User $user = null)
    {
        $user = $user ? : factory(User::class)->create();

        $this->actingAs($user);
        $token = JWTAuth::fromUser($user);
        $this->withHeader('Authorization', 'Bearer ' . $token);

        return $user;
    }
yasming commented 3 years ago

Hello ! Maybe you already founded the solution but if not try this: Screen Shot 2020-11-11 at 6 49 45 PM

the path for the classes changed, worked for me use the news paths on $routeMiddleware:

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

hashtag77 commented 3 years ago

It really helps for latest versions. Thanks @yasming