Request authorize will throw an AccessDeniedException Error in my case
class IndexRequest extends FormRequest
{
public function authorize()
{
return auth()->user()->can('list-foos');
}
}
From that, route bind on index function
public function index(IndexRequest $request)
In Handler class,
$this->reportable(function (AccessDeniedHttpException $e) {
return $this->response('unauthorized', 403);
});
But
"message": "This action is unauthorized.", "exception":"Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException", "file":"/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php", "line": 356,
was thrown as a response
I have tried to catch with exception from Spatie documentation
https://spatie.be/docs/laravel-permission/v5/advanced-usage/exceptions
CodePudding user response:
Found out. I have to use render() function instead of register() and
Illuminate\Auth\Access\AuthorizationException
public function render($request, Throwable $exception)
{
if ($exception instanceof AuthorizationException) {
return $this->responseUnauthroized('Unauthorized');
}
}