For my web service using FOSRestBundle, I created a firewall that forces a login to access the application.
My problem is that when I make a call to the API via ajax, I need to get the error code 401 when the user is not authenticated rather than receive the html source code of the login form. How do I configure the application?
secured_area:
pattern: ^/
form_login:
provider: fos_userbundle
use_forward: false
default_target_path: /w
logout:
path: /logout
target: /login
EDIT:
Thanks to Ryan here is the KernelExceptionListener method.
public function onKernelException( GetResponseForExceptionEvent $event ) {
// get exception
$exception = $event->getException();
// get path
$path = $event->getRequest()->getPathInfo();
if ( $exception instanceOf AuthenticationException && ($event->getRequest()->isXmlHttpRequest() || strpos( $path, '/api' ) === 0) ) {
$response = new Response();
$response->setStatusCode( 401 );
$event->setResponse( $response );
$event->stopPropagation();
}
}