1

When overriding FOSUserBundle resetting password controller, there is a function call to "authenticateUser" method (line 104) :

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/ResettingController.php#L104

....
$this->authenticateUser($user);
....

My problem is that I already override the Symfony authentication handler, and have my own logic when a user logs in.

EDIT Here is my authentication handler :

<?php

/* ... all includes ... */

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
    private $router;
    private $container;

    public function __construct(Router $router, ContainerInterface $container)
    {
        $this->router = $router;
        $this->container = $container;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        // retrieve user and session id
        $user = $token->getUser();

        /* ... here I do things in database when logging in, and dont want to write it again and again ... */

        // prepare redirection URL
        if($targetPath = $request->getSession()->get('_security.target_path')) {
            $url = $targetPath;
        }
        else {
            $url = $this->router->generate('my_route');
        }

        return new RedirectResponse($url);
    }

}

So, How could I call the "onAuthenticationSuccess" method from my authentication handler in the ResettingController ? In order to avoid rewriting the same code...

Thanks for your help !

Aurel

2
  • could you post your custom auth handler? Commented May 25, 2012 at 12:00
  • see my edit in my question... Commented May 25, 2012 at 14:28

1 Answer 1

1

You should call your onAuthenticationSuccess method loading it as a service. In your config.yml:

authentication_handler:
    class: Acme\Bundle\Service\AuthenticationHandler
    arguments:
        container: "@service_container"

And then, call it in the authenticateUser function:

protected function authenticateUser(UserInterface $user) {
      try {
        $this->container->get('fos_user.user_checker')->checkPostAuth($user);
      } catch (AccountStatusException $e) {
          // Don't authenticate locked, disabled or expired users
          return;
      }

      $providerKey = $this->container->getParameter('fos_user.firewall_name');
      $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
      $this->container->get('security.context')->setToken($token);
      $request = $this->container->get('request');
      $this->container->get('authentication_handler')->onAuthenticationSuccess($request, $token);
}

this do the trick and pass through your custom auth handler. More info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.