I'm on the dev-develop branch. I just upgraded to Laravel 5.4 and now I get errors that say that jwt.auth doesn't exist.
ReflectionException in Container.php line 681:
Class jwt.auth does not exist
I have checked the LaravelServiceProvider and made sure I registered it in my app.php (this all works before I upgraded to 5.4)... this is the line that I'm assuming should create that middleware option:
$this->app['router']->middleware('jwt.auth', Authenticate::class);
I tried creating a middleware group like this:
'auth:api' => [
'throttle:200,1',
'jwt.auth',
],
but I of course get the error...
I have gotten it to work by using the middleware class directly instead of trying to use the name.
'auth:api' => [
'throttle:200,1',
Authenticate::class
],
So that is a workaround.. but I'm guessing this is not working as intended.
Strange, the only thing I had to change was the provider in app.php (changed to Tymon\JWTAuth\Providers\LaravelServiceProvider::class.
This is probably known already but it's my first time working from the dev branch.
Edit: While this was necessary it wasn't the real issue See below.
@wuori Are you on laravel 5.4? And you can use 'jwt.auth' in your middleware?
I just retried installing everything just to make sure... and I still can't use jwt.auth middleware for the dvelop branch on 5.4.
jwt it's not 100% compatible with laravel 5.4 yet see https://github.com/tymondesigns/jwt-auth/pull/969
@isaackearl Yes, just upgraded to 5.4 when I posted the comment. Verified version just in case, as it seemed a little odd to have it work without making other changes. Still using older middleware syntax in the route:
Route::group(
['middleware' => ['auth']], function () {
Update: I was wrong, looking at the wrong endpoint that isn't behind auth. See below comment for more.
@wuori if you look at the LaravelServiceProvider class you'll see where the name 'jwt.auth' is registered for the Authenticate class... I was trying to use this name in order to use the middleware. This worked fine before the upgrade. Like this:
$this->app['router']->middleware('jwt.auth', Authenticate::class);
Because of this I've always been able to use jwt.auth as a middleware like this:
Route::group(
['middleware' => ['jwt.auth']], function () {
I see your says 'auth' instead of jwt.auth... is that because you have a middleware group called 'auth' that includes jwt.auth ? Sorry for all the questions I'm just trying to understand!
Thanks.
@isaackearl Ok, so I... lied. Sorry! I was testing a login endpoint (uses JWTAuth::attempt). Will update my comments to reflect my ignorance...
However, I updated Kernal with:
'jwt.auth' => 'Tymon\JWTAuth\Http\Middleware\Authenticate' instead of
'jwt.auth' => 'Tymon\JWTAuth\Http\Middleware\GetUserFromToken'
That allows me to keep using:
Route::group(['middleware' => ['jwt.auth']], function () {
I'm now able to test a protected endpoint by using $token = \JWTAuth::getToken(); and \JWTAuth::parseToken()->authenticate() from the docs.
There's still some errors with exceptions (401 in particular) I'm trying to figure out but that could be due to some other updates I made.
Not sure if this is complete but it's helping me get on the right path.
@wuori I've found the fix for the issue and created a pull request. It ends up the middleware function used in the LaravelServiceProvider doesn't work anymore in Laravel 5.4... and instead the aliasMiddleware function must be called instead. That is why 'jwt.auth' was not being added to the alias middleware for me.
Pull request #976 fixes this issue for Laravel
I'm not sure if this issue exists for Lumen.
App\Exceptions\Handler.php
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) {
return response()->json(['token_expired'], $e->getStatusCode());
} else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException) {
return response()->json(['token_invalid'], $e->getStatusCode());
}
when token invalid I got
UnauthorizedHttpException in BaseMiddleware.php line 71:
Token Signature could not be verified.
instead of json 'token_invalid', How to fix?
I got the same unhandled exception:
Whoops, looks like something went wrong.
2/2 UnauthorizedHttpException in BaseMiddleware.php line 71: Token has expired
instead of my json
if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException) {
$data['code'] = -1;
$data['message'] = 'token_expired';
return response()->json($data, $e->getStatusCode());
The same issue is happening for lumen 5.4 also.
I have written all the providers, class_alias and routeMiddleware also.
i am getting the same error.
ReflectionException in Container.php line 681:
Class jwt.auth does not exist.
If you have solved it could you please help me out of this.
Thanks in Advance
i've solved in a dirty way for now.
but it works for me
if ($e instanceof JWTException || str_contains($e->getMessage(), 'Token not provided')) {
$data['code'] = -104;
$data['message'] = 'token_not_provided';
return response()->json($data, $e->getStatusCode());
} else if ($e instanceof TokenExpiredException || str_contains($e->getMessage(), 'Token has expired')) {
$data['code'] = -105;
$data['message'] = 'token_expired';
return response()->json($data, $e->getStatusCode());
} else if ($e instanceof TokenInvalidException || str_contains($e->getMessage(), 'is an invalid JWS')) {
$data['code'] = -106;
$data['message'] = 'token_invalid';
return response()->json($data, $e->getStatusCode());
}
Most helpful comment
i've solved in a dirty way for now.
but it works for me