I know this post is a little dated, but it still came up as one of the first results on Google.
The answers in this post reference the SecurityContext
class, which is no longer supported as of Symfony 2.6. The accepted answer for this post is misleading because of the class deprecation.
Try this code from this answer:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use YourNameSpace\UserBundle\Entity\User;
class LoginController extends Controller{
public function registerAction()
{
$user = //Handle getting or creating the user entity likely with a posted form
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
}
}