Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/176051d5e57ebda404e2 to your computer and use it in GitHub Desktop.
Save anonymous/176051d5e57ebda404e2 to your computer and use it in GitHub Desktop.
<?php
/*
* This file is part of the Welcome project.
*
* (c) Chris BECKER <goabonga@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Security\Http\Authentication;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Http\HttpUtils;
class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
{
private $router;
public function __construct(RouterInterface $router, HttpKernelInterface $httpKernnel, HttpUtils $httpUtils)
{
$this->router = $router;
parent::__construct($httpKernnel, $httpUtils, array());
}
/**
* @inheritDoc
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->isXmlHttpRequest()) {
$array = array('success' => false, 'message' => $exception->getMessage());
$response = new Response(json_encode($array), 401);
$response->headers->set('Content-Type', 'application/json');
return $response;
} else {
if (false === $this->options['failure_forward']) {
$url = $this->httpUtils->generateUri(
$request,
$referer = $request->headers->get('Referer', $this->options['login_path'])
);
$this->options['failure_path'] = $url;
return parent::onAuthenticationFailure($request, $exception);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment