Jwt-auth: Publish middleware for custom responses

Created on 8 Jun 2017  路  3Comments  路  Source: tymondesigns/jwt-auth

In the wiki under the Authentication section there is info for Middleware and Filters.

Now these are references i.e. as 'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken'

When this is used I have no way (other than making changes in the package) of modifying how the response is assembled. It's this i.e. in the BaseMiddleware@response:

 return $response ?: $this->response->json(['error' => $error], $status);

Would you change this and publish the middleware and reference to the App\Http\Middleware folder so that we have a way to modify how the response is assembled according to our structure?

Is there a better way to do this?

Most helpful comment

Great, works.

The documentation didn't mention where to add this. After looking here it's clear: https://laravel.com/docs/5.4/events

Add these lines in app/Providers/EventServiceProvider.php

Event::listen('tymon.jwt.invalid', function () {
    return \Response::make(['your_custom_error' => 'token invalid'], 400);
});

Thanks again Zerazeru.

All 3 comments

It seems to me that the only way to customize the response is by defining event listeners.

Event::listen('tymon.jwt.invalid', function () {
    return Response::make(['your_custom_error' => 'token invalid'], 400);
});

Thanks Zerazeru, I'll try this out and close the issue for now.

Great, works.

The documentation didn't mention where to add this. After looking here it's clear: https://laravel.com/docs/5.4/events

Add these lines in app/Providers/EventServiceProvider.php

Event::listen('tymon.jwt.invalid', function () {
    return \Response::make(['your_custom_error' => 'token invalid'], 400);
});

Thanks again Zerazeru.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NaelsonBrasil picture NaelsonBrasil  路  3Comments

johncloud200 picture johncloud200  路  3Comments

loic-lopez picture loic-lopez  路  3Comments

harveyslash picture harveyslash  路  3Comments

CBR09 picture CBR09  路  3Comments