How to login a user programatically in Symfony2

8 thoughts on “How to login a user programatically in Symfony2”

  1. Good post,this is a pretty complex topic and many people need to do this kind of stuff. By the way, to login a user using something different than a login form, you should implement an authentication provider. The logic involved is quite the same you wrote, but you should put it in a SecurityListener rather than in a controller. There is a good cookbook for this http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html and I wrote a post about this topic http://www.screenfony.com/blog/symfony-custom-authentication-provider

  2. Hi Hasin, very good post! I am trying do a form login, but i had followed the Security page article Symfony and it work not my project.
    Any help thank!

  3. I have a problem! when the user login, This line does not run
    if ($this->get(‘security.context’)->isGranted(‘ROLE_ADMIN’)) {
    return $this->redirect($this->generateUrl(‘cituao_coord_homepage’));

    why? any help! Thanks

  4. I think it’d be better to use Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN constant instead of the hardcoded event name.

    Good post!

  5. Developing an App where only OAuth is supported. So was a hassle to work without internet. Then this post helped me to simulate user authentication manually.

    Thanks Hasin vai🙂

  6. Do you know of a way to login users against an LDAP server. Every LDAP bundles seems to required a local database. I dont need a persistent local database in Symfony2. I want to authenticate and fill a user class from an LDAP server.

    This is the point of LDAP after all. Any clues of how to do that, I would be grateful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s