Skip to content Skip to sidebar Skip to footer

FOSUserBundle AJAX Login With Symfony2 (routing)

I'm trying to make the AJAX authentication work with FOSUserBundle. I have created an Handler directory with a AuthenticationHandler class :

Solution 1:

First Issue: You are sending an invalid CSRF token. In Symfony 2.3 you could generate it using {{ csrf_token('authenticate') }} inside the template's input's value.

Second issue: Do not overwrite the route, simply use the original route: fos_user_security_check.

In general: if you use an AuthenticationSuccessHandler extending Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler your method could look something like this:

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    if ($request->isXmlHttpRequest()) {
        return new JsonResponse(array('success' => true));
    }

    return parent::onAuthenticationSuccess($request, $token);
}

Do something similar for an AuthenticationFailureHandler extending Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler.


Post a Comment for "FOSUserBundle AJAX Login With Symfony2 (routing)"