jwt-auth icon indicating copy to clipboard operation
jwt-auth copied to clipboard

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

Open yousefAK opened this issue 4 years ago • 13 comments

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,
        ],
    ],

yousefAK avatar Aug 09 '20 15:08 yousefAK

Same issue here.

nicolas-costa avatar Aug 19 '20 21:08 nicolas-costa

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

dzung1nguyen avatar Sep 12 '20 18:09 dzung1nguyen

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

AAmeriyan avatar Nov 03 '20 05:11 AAmeriyan

Same problem here while installing Laravel Masterpass plugin.

hembachrterran avatar Nov 03 '20 10:11 hembachrterran

same here

mohamadrezapishdad avatar Nov 04 '20 16:11 mohamadrezapishdad

no solutions yet ?

mohamadrezapishdad avatar Nov 12 '20 09:11 mohamadrezapishdad

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.

totymedli avatar Nov 19 '20 14:11 totymedli

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

facueche avatar Jan 06 '21 15:01 facueche

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

RichieMcMullen avatar Jan 30 '21 22:01 RichieMcMullen

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

IDLKZ avatar Aug 15 '21 05:08 IDLKZ

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',
    ],`

AbdulrazakZakieh avatar Aug 30 '21 13:08 AbdulrazakZakieh

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

anyone found the solution?

VirtumartOz avatar Sep 06 '21 09:09 VirtumartOz

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.

dnkmdg avatar Oct 14 '21 18:10 dnkmdg