I am working on laravel passport package. When i revoke token and access the authenticated endpoint it throws an exception.
The logs file contain "The resource owner or authorization server denied the request". To handle is exception i created OAuth middleware and placed exception code in it as mentioned in this link: https://www.kingpabel.com/oauth2-exception-custom-error-message/
public function handle($request, Closure $next)
{
//return $next($request);
try {
$response = $next($request);
// Was an exception thrown? If so and available catch in our middleware
if (isset($response->exception) && $response->exception) {
throw $response->exception;
}
return $response;
} catch (OAuthException $e) {
$data = [
// 'error' => $e->errorType,
// 'error_description' => $e->getMessage(),
'error' => 'Custom Error',
'error_description' => 'Custom Description',
];
return \Response::json($data, $e->httpStatusCode, $e->getHttpHeaders());
}
}
I want to return the error in json format like:
{
"error": "Token is invalid!"
}
I will appreciate if anyone guide me in this regard. Thanks,