Search code examples
laravellaravel-5oauthlaravel-5.4laravel-passport

Changing The Laravel Passport/OAuth-Server Responses


I'm building an API in Laravel 5.4, using Laravel Passport 3 for authentication. All of my API methods return a set of values that are always returned, success, errors (if there are any errors) etc.

I've changed the response of a \Illuminate\Auth\AuthenticationException throws, to fit with the rest of my app, however I'm not sure how to change the response of various token grant responses, without doing something horrible like editing the vendor files.


Solution

  • I think you can use middleware to change your response.

    From laravel documentation:

    Before & After Middleware

    Whether a middleware runs before or after a request depends on the middleware itself.

    You can capture the response and re-format the response.

    You can use laravel's setContent method to set the content in response. Check here.