tymondesigns / jwt-auth

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

Auth driver [jwt] for guard [api] is not defined #2015

Open yousefAK opened 4 years ago

yousefAK commented 4 years ago

Auth driver [jwt] for guard [api] is not defined.

Your environment

Q A
Bug? no
New Feature? no
Framework Lumen
Framework version 6.3.5
Package version 1.0.0-rc.5
PHP version 7.2

My Application was working without any error, i am using lumen 6.3.5 and jwt 1.0.0-rc5 when i deploy the application again and make composer install, i get this error

PHP Fatal error:  Uncaught InvalidArgumentException: Auth driver [jwt] for guard [api] is not defined. in /var/www/html/proj/vendor/illuminate/auth/AuthManager.php:97
Stack trace:
#0 /var/www/html/proj/vendor/illuminate/auth/AuthManager.php(68): Illuminate\Auth\AuthManager->resolve('api')
#1 /var/www/html/proj/vendor/illuminate/auth/AuthManager.php(307): Illuminate\Auth\AuthManager->guard()
#2 /var/www/html/proj/app/Exceptions/Handler.php(44): Illuminate\Auth\AuthManager->__call('user', Array)
#3 /var/www/html/proj/vendor/sentry/sentry/src/State/Hub.php(98): App\Exceptions\Handler->App\Exceptions\{closure}(Object(Sentry\State\Scope))
#4 /var/www/html/proj/vendor/sentry/sentry/src/functions.php(78): Sentry\State\Hub->configureScope(Object(Closure))
#5 /var/www/html/proj/app/Exceptions/Handler.php(48): Sentry\configureScope(Object(Closure))
#6 /var/www/html/proj/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(122): App\Exce in /var/www/html/proj/vendor/illuminate/auth/AuthManager.php on line 97

now i cannot make any artisan command like (php artisan cache:clear) every time i try to do anything i get the same error. by the way the code was working 100% and i was using jwt.

this is my bootstrap/app.php:


$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
    'throttle' => App\Http\Middleware\ThrottleRequests::class,
]);
$app->withFacades(true, [
    Tymon\JWTAuth\Facades\JWTAuth::class => 'JWTAuth',
    Tymon\JWTAuth\Facades\JWTFactory::class => 'JWTFactory',
]);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);

and this is the auth file:

'defaults' => [
        'guard' => env('AUTH_GUARD', 'api'),
        'passwords' => 'users',
    ],
'guards' => [
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],
nicolas-costa commented 4 years ago

Same issue here.

dzung1nguyen commented 3 years ago

Laravel 8.0 PHP 7.4 tymon/jwt-auth: ^1.0

In laravel 8.0, same issue if i set default to "api", but if i use Auth::guard('api'), the error will be "Method Illuminate\Auth\SessionGuard::factory does not exist."

AAmeriyan commented 3 years ago

Same here!!!!!!Any answer???

hembachrterran commented 3 years ago

Same problem here while installing Laravel Masterpass plugin.

mohamadrezapishdad commented 3 years ago

same here

mohamadrezapishdad commented 3 years ago

no solutions yet ?

totymedli commented 3 years ago

For me this happened after I upgraded to Laravel 7. According to the upgrade guide App\Exceptions\Handler now uses Throwable instead of Exception. If you have traits used in this handler, update those too.

facueche commented 3 years ago

This worked for me https://github.com/tymondesigns/jwt-auth/issues/1886#issuecomment-538448309 on Laravel 6.0

RichieMcMullen commented 3 years ago

Leave the default guard as it is and do this:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
        'hash' => false,
    ],
],

Then just call:

auth('api')->attempt([....etc
IDLKZ commented 3 years ago

Leave the default guard as it is and do this:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
        'hash' => false,
    ],
],

Then just call:

auth('api')->attempt([....etc

THANKS FRIEND

AbdulrazakZakieh commented 3 years ago

I solved the problem by adding this line to the providers in app.php Tymon\JWTAuth\Providers\LaravelServiceProvider::class, and here is guards I have: ` 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api_customer' => [
         'driver' => 'jwt',
         'provider' => 'api_customer',
    ],`
VirtumartOz commented 3 years ago

after following @RichieMcMullen 's solution now my error becomes Method Illuminate\Auth\SessionGuard::factory does not exist.

anyone found the solution?

dnkmdg commented 2 years ago

after following @RichieMcMullen 's solution now my error becomes Method Illuminate\Auth\SessionGuard::factory does not exist.

anyone found the solution?

If you follow @RichieMcMullen's solution and then also add the ServiceProvicer as @AbdulrazakZakieh suggestes it will work fine.