Editing Laravel Passport / OAuth-Server responses

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

I changed the response of \Illuminate\Auth\AuthenticationExceptionthrows to fit the rest of my application, however I am not sure how to change the response to the various responses to the token output without doing something terrible, like editing the file provider.

+6
source share
3 answers

I think you can use middleware to change your answer.

From the laravel documentation:

Before and after middleware

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

You can record the answer and reformat the answer.

You can use the laravel method setContentto set the content in response. Check here .

+1
source

What you are trying to do here is not supported by the library, so everything you do will be hacky and will probably break compatibility with future versions laravel/passport.

In my opinion, you can choose only two options:

  • (Passport::routes()) . Passport, , . , , (success error), .

  • laravel/passport . , , , , .

, . - : , , .

+2

Another way is to create proxy routes for your purposes.

Route::post('custom-auth/token', function (Request $request) {
    $proxy = Request::create('oauth/token', 'POST', $request->request->input());

    $response = app()->handle($proxy);

    return responseCallback($response);
});

Where is responseCallbackyour custom response modifier function.

-1
source

All Articles