symfony2 ajax,php - Symfony2 AJAX Login - Stack Overflow

I have an example where I am trying to create an AJAX login using Symfony2 and FOSUserBundle. I am setting my own success_handler and failure_handler under form_login in my security.yml file.

Here is the class:

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->isXmlHttpRequest()) {

$result = array('success' => true);

$response = new Response(json_encode($result));

$response->headers->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->isXmlHttpRequest()) {

$result = array('success' => false, 'message' => $exception->getMessage());

$response = new Response(json_encode($result));

$response->headers->set('Content-Type', 'application/json');

return $response;

}

}

}

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:

Catchable Fatal Error: Argument 1 passed to Symfony\Component\HttpKernel\Event\GetResponseEvent::setResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given

I'd like for my onAuthenticationSuccess and onAuthenticationFailure overrides to only be executed for XmlHttpRequests (AJAX requests) and to simply hand the execution back to the original handler if not.

Is there a way to do this?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>