adit
adit

Reputation: 33674

onAuthenticationSuccess giving me events as parameters

I have the following handler on authentication success:

app_auth_success_handler:
        class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler
        public: true
        arguments: ['@router', "@service_container"]
        tags:
             - { name: kernel.event_listener, event: security.authentication.success, method: onAuthenticationSuccess }

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
    private $router;
    private $container;

    /**
    * Constructor
    * @param RouterInterface   $router
    */
    public function __construct(RouterInterface $router, $container)
    {
        $this->router = $router;
        $this->container = $container;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {

However after I authenticate the user it gives me this error:

Catchable fatal error: Argument 1 passed to App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler::onAuthenticationSuccess() must be an instance of Symfony\Component\HttpFoundation\Request, instance of Symfony\Component\Security\Core\Event\AuthenticationEvent given in /Users/John/Sites/App/src/Shopious/UserBundle/Security/User/Handler/LoginAuthSuccessHandler.php on line <i>31</i></th></tr>

Can't figure out why this is..

Upvotes: 1

Views: 1046

Answers (1)

Emii Khaos
Emii Khaos

Reputation: 10085

You are mistaking the security.authentication.success event with the success_handler which you can define in your firewall config.

The service for a success_handler can be any service implementing AuthenticationSuccessHandlerInterface.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token) {}
}

It doesn't need to tagged as an event listener:

app_auth_success_handler:
    class: App\UserBundle\Security\User\Handler\LoginAuthSuccessHandler
    public: true
    arguments: ['@router', "@service_container"]

Instead the service name is passed to the firewall config:

security:
    firewalls:
        main:
            form_login:
                success_handler: app_auth_success_handler

Upvotes: 2

Related Questions