Amir Anvarov
Amir Anvarov

Reputation: 107

JWT, Laravel Class Tymon\JWTAuth\MiddlewareGetUserFromToken does not exist

I set up JWT (tymondesigns/jwt-auth) on my laravel 5.0.35 app. Authentication works great. But, when i use jwt.auth middleware there is an error: exception 'ReflectionException' with message 'Class Tymon\JWTAuth\MiddlewareGetUserFromToken does not exist'

Your help would be appreciated))) Here are files that might be helpfull for you:

app.php

$providers = [
    // other records
    'Tymon\JWTAuth\Providers\JWTAuthServiceProvider',
];

'aliases' => [
    // other records
    'JWTAuth'   => 'Tymon\JWTAuth\Facades\JWTAuth',
    'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory'
];

kernel.php

protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    'jwt.auth' => 'Tymon\JWTAuth\MiddlewareGetUserFromToken',
    'jwt.refresh' => 'TymonJWTAuth\MiddlewareRefreshToken'
];

Contoller where I call jwt.auth middleware

namespace App\Http\Controllers\Resources;

use IlluminateHttpRequest;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\User;


use PDO;
use Log;

class DirectionsController extends Controller {

    public function __construct()
   {
       $this->middleware('jwt.auth');
   }

compsoser.json

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "laravel/framework": "5.0.*",
    "guzzlehttp/guzzle": "~6.0",
    "rap2hpoutre/laravel-log-viewer": "0.2.*",
    "irazasyed/telegram-bot-sdk": "^2.0",
    "tymon/jwt-auth": "0.5.*"
},
"require-dev": {
    "phpunit/phpunit": "~4.0",
    "phpspec/phpspec": "~2.1"
},
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},
"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php -r \"copy('.env.example', '.env');\"",
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
}

}

Upvotes: 1

Views: 3674

Answers (1)

Amir Anvarov
Amir Anvarov

Reputation: 107

I have found the error. The error is where there is a declaration of middleware in kernel.php

'jwt.auth' => 'Tymon\JWTAuth\MiddlewareGetUserFromToken',
'jwt.refresh' => 'TymonJWTAuth\MiddlewareRefreshToken'

So, instead of MiddlewareGetUserFromToken should be Middleware\GetUserFromToken. Looks like this type was in blog post where I copied it. Be careful when your copy code from other websites)

Upvotes: 6

Related Questions