title: Symfony2 AJAX Login
body: <p>I have an example where I am trying to create an AJAX login using Symfony2 and FOSUserBundle. I am setting my own <code>success_handler</code> and <code>failure_handler</code> under <code>form_login</code> in my <code>security.yml</code> file.</p>
  
  <p>Here is the class:</p>
  
  <pre><code>class AjaxAuthenticationListener implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
  {  
      /**
       * This is called when an interactive authentication attempt succeeds. This
       * is called by authentication listeners inheriting from
       * AbstractAuthenticationListener.
       *
       * @see \Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener
       * @param Request        $request
       * @param TokenInterface $token
       * @return Response the response to return
       */
      public function onAuthenticationSuccess(Request $request, TokenInterface $token)
      {
          if ($request-&gt;isXmlHttpRequest()) {
              $result = array('success' =&gt; true);
              $response = new Response(json_encode($result));
              $response-&gt;headers-&gt;set('Content-Type', 'application/json');
              return $response;
          }
      }
  
      /**
       * This is called when an interactive authentication attempt fails. This is
       * called by authentication listeners inheriting from
       * AbstractAuthenticationListener.
       *
       * @param Request                 $request
       * @param AuthenticationException $exception    
       * @return Response the response to return
       */
      public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
      {
          if ($request-&gt;isXmlHttpRequest()) {
              $result = array('success' =&gt; false, 'message' =&gt; $exception-&gt;getMessage());
              $response = new Response(json_encode($result));
              $response-&gt;headers-&gt;set('Content-Type', 'application/json');
              return $response;
          }
      }
  }
  </code></pre>
  
  <p>This works great for handling both successful and failed AJAX login attempts. However, when enabled - I am unable to login via the standard form POST method (non-AJAX). I receive the following error:</p>
  
  <p><code>Catchable Fatal Error: Argument 1 passed to Symfony\Component\HttpKernel\Event\GetResponseEvent::setResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given</code></p>
  
  <p>I'd like for my <code>onAuthenticationSuccess</code> and <code>onAuthenticationFailure</code> overrides to only be executed for XmlHttpRequests (AJAX requests) and to simply hand the execution back to the original handler if not.</p>
  
  <p>Is there a way to do this?</p>
  
  <p>TL;DR I want AJAX requested login attempts to return a JSON response for success and failure but I want it to not affect standard login via form POST.</p>