FOSUserBundle AJAX Login with Symfony2 (routing)

后端 未结 1 492
终归单人心
终归单人心 2021-01-03 08:56

I\'m trying to make the AJAX authentication work with FOSUserBundle.

I have created an Handler directory with a AuthenticationHandler class :

         


        
相关标签:
1条回答
  • 2021-01-03 09:15

    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.

    0 讨论(0)
提交回复
热议问题