I have an application running with Symfony5 and ApiPlatform using JWT in which I want some user not to be able to log in under some condition
So I have a listener LoginListener as follow:
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
$user = $event->getUser();
if ($user->someCondition) {
throw new MyAccessDeniedException(json_encode('Ce compte n\'est plus actif.'));
}
}
The exception class used is only that :
<?php
namespace App\Exception;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class MyAccessDeniedException extends AccessDeniedException
{
}
I use this exception to throw a 403 pretty often in different cases, including in some other listeners.
The issue here is that, instead of returning an exception under json format, easily usable by the frontend, it returns an html page exception from symfony.
For example this is the kind of issue I'm trying to return, istead of the HTML :
"@context":"\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"Some error message","trace":[{"namespace":"","short_class":"","class":"",.....}
Does anyone knows why, on this particular exception the render of the exception turn out to be HTML and not JSON ? And if there is a workaround to this issue ?
Thanks !