I’ll just share here a quick snippet that serves to pragmatically authenticate the user in Symfony2.
It is very useful, especially after checking an email account using a token for example. In this case you might don’t want to ask again the password to your user and do the authentication for him.
What you need to do is to manually create the AuthenticationToken and give it to the security context:
// create the authentication token $token = new UsernamePasswordToken( $user, null, 'main', $user->getRoles()); // give it to the security context $this->container->get('security.context')->setToken($token);
Note that the third parameter of the token constructor is the name of the security provider associated with your user found in your app/config/security.yaml file.