Do something just after symfony2 login success and before redirect?

12,984

You can specify a login success handler to be executed on successful login.

For example, your security.yml

firewalls:
    main:
        pattern: ^/
        form_login:
            success_handler: my.security.login_handler

Now create the class which implements Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface and on successful login, you can do whatever you need and handle the redirect as you see fit.

/**
 * 
 */
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    // handle it and return a response
}

Then create a service with that name in your services.xml for your bundle, or in your config.yml using the newly created handler.

I originally found out how to do this following this tutorial:

http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

Share:
12,984
piotrekkr
Author by

piotrekkr

Updated on June 03, 2022

Comments

  • piotrekkr
    piotrekkr almost 2 years

    I'm searching for a while now, for any info, on how to do something after authentication success in symfony2. I want to rehash user password to use bcrypt just after successful authentication using old hash. I need to do this when I still have valid plain password so it should be just after credentials check and before redirect.

    Any clues how to achieve that?

    I found something about event dispatcher in Symfony but I can't find if there is any event after successful authentication.

    Please correct me if I'm trying to do this wrong way and suggest some better approach.

    //EDIT

    Ok I found event fired just after auth success, it's called security.authentication.success. So i can now attach to this event but now I'm not sure where in my boundle code should I attach my event listener? Should I do that in my /src/Pkr/BlogUserBundle/DependencyInjection/PkrBlogUserExtension.php in load() method?

  • piotrekkr
    piotrekkr about 11 years
    Great, I'll try it shortly :)
  • piotrekkr
    piotrekkr about 11 years
    Your solution works great. At first I tried with event listener attached to login success event but then I didn't have request object so easy accessible as in your solution :) Thank you for help
  • adavea
    adavea about 10 years
    When I tried this I found $request->headers->get('referer') was set to my login page. Any idea how to get the page requested before the user was redirected to the login page?
  • Ghassan Idriss
    Ghassan Idriss about 10 years
    You could try to edit the login controller and maybe set the referrer on that screen in the session and then access it and clean up in your login handler.
  • ssss
    ssss about 9 years
    How would one access the session or the doctrine from within such a login success handler?
  • Ghassan Idriss
    Ghassan Idriss about 9 years
    Since this is a service, you would use dependency injection to inject whatever other services you require in your constructor or setter method. See symfony.com/doc/current/components/dependency_injection/…
  • piotrekkr
    piotrekkr over 8 years
    Since Symfony 2.8 there is a Guard authenticator service which aims to simplify creating custom authentication system. There is onAuthenticationSuccess() method which can return RedirectResponse but also can contain some custom logic . It's worth a look if building custom authentication process. Tutorial here sitepoint.com/easier-authentication-with-guard-in-symfony-3