-1

I have followed following guide to make an Authentication Api, and an api which uses the authentication from the first one. I have done everything exactly the same, but i still get this error:

InvalidArgumentException: Auth guard [jwt] is not defined. in file folder/to/project/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 84

The guide i have followed is: https://medium.com/swlh/build-jwt-authentication-between-multiple-api-with-laravel-fbd03352aaa3

My config/auth looks like this:

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

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

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

Then my app/guard/JWTGuard.php looks exactly like in the guide. My routes/api.php look the following:

Route::middleware('auth:jwt')->resource('projects', ProjectController::class);

Does anybody has this issue before and know how to solve it?

Guus
  • 1
  • 2
  • I'm not famliar with this problem specifically in Laravel, but this is a common issue in all plataforms. For jwt CORS protection to work, it has to receive a JWT/CORS token, that is sent back in the next request. The normal solutions are: Or define an specific field in your page (the name is important, normally is _cors or something similar) OR deactivate CORS protection. – Sourcerer Jan 21 '21 at 18:02
  • Is it cors? Really dont think this is related to cors errors – Guus Jan 21 '21 at 18:14
  • Cannot be sure, but the message make me to suspect this. You have several questions in SO about this, e.g. https://stackoverflow.com/questions/54477420/how-to-enable-both-api-and-web-guard-in-laravel – Sourcerer Jan 21 '21 at 18:18
  • Ill look into it – Guus Jan 21 '21 at 18:20
  • If you find the exact solution, don't forget to answer your own question, for help future users! – Sourcerer Jan 21 '21 at 18:23
  • Welcome to SO ... the guard you defined is named `api` not `jwt` ... `auth:api` – lagbox Jan 21 '21 at 18:27
  • Okey thanks lagbox, but then laravel will whine about route login not defined. – Guus Jan 21 '21 at 18:37
  • because you are using the correct guard now but you are sending the wrong headers with your request – lagbox Jan 21 '21 at 18:40
  • Which headers do you mean? I only send the Authorization header – Guus Jan 21 '21 at 18:41
  • you need to send headers to the application with the request to tell the application you are accepting JSON for the response – lagbox Jan 21 '21 at 18:45
  • Iam doing that. I make my requests with Postman. I made an named route called login, which only returns "Unauthorized". So when my jwt token isnt correct, i will get redirected to that route. But now i always get there, even with a correct token. I am a newbie with laravel so not so much experience – Guus Jan 21 '21 at 18:47

1 Answers1

0

The problem was in routes/api.php. The middleware shouldn't be "auth:jwt", but "auth:api". This did the trick for me.

Guus
  • 1
  • 2