oauth2 custom error message

Oauth2 Exception Custom Error Message

While using oauth2 with laravel there is a problem for response custom error message instead of oauth2 error format.For details how to use oauth2 with laravel please go here.This happens if your api error format is different rather that oauth2 error format.I fall this types of problem and i solved it.Here we know how we response custom error message while using oauth2.Default oauth2 exception middleware is

\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class

We are going to remove that middleware and we wrote a new middleware.So first create a middleware in app/Http/Middleware.My middleware name is OauthExceptionMiddleware

Then open

app/Http/kernel.php

and put this middleware instead of oauth2 previous middleware in $middleware array,like this

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\OauthExceptionMiddleware::class,
    ];

Oauth2 Custom Exception Error Message

and now check your error message,then it will show demo error message,now anyone can format his error message in this middleware how he want.

Happy coding!!!