I would suggest to use an entry point and a success handler.
security.yml:
firewalls: # Required
# Examples:
somename:
entry_point: some.service.id
...
form_login:
...
success_handler: some.service.id
SuccessHandler (source):
<?php
namespace StatSidekick\UserBundle\Handler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {
public function __construct( HttpUtils $httpUtils, array $options ) {
parent::__construct( $httpUtils, $options );
}
public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
// Create if necessary a redirect response otherwise use the parent one with
// $response = parent::onAuthenticationSuccess( $request, $token );
return $response;
}
}
Entry point (source):
When the user is not authenticated at all (i.e. when the token storage
has no token yet), the firewall's entry point will be called to
"start" the authentication process. An entry point should implement
AuthenticationEntryPointInterface, which has only one method: start() ...