I would like to have some action after a success authentification.
I created a class extended DefaultAuthenticationSuccessHandler, and redefined the onAuthenticationSuccess function.
Though the authentication is a success, the programm never goes to the function : why ?
namespace my_name_space;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Bridge\Monolog\Logger;
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {
private $em;
private $logger;
function __construct(Logger $logger, HttpUtils $httpUtils, EntityManager $em) {
parent::__construct($httpUtils);
$this->em = $em;
$this->logger = $logger;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
$this->logger->info("onAuthenticationSuccess");
$response = parent::onAuthenticationSuccess($request, $token);
$token->eraseCredentials();
$token->getUser()->addRole('ROLE_USER');
return $response;
}
}
services:
security.authentication.success.handler:
class: MY\NAMESPACE\AuthenticationSuccessHandler
arguments: ["@monolog.logger.php","@security.http_utils", "@doctrine.orm.default_entity_manager"]
security.yml
form_login:
check_path: _security_check
use_referer: true
use_forward: true
login_path: my_login
success_handler: security.authentication.success.handler