I have a controller that is supposed to add a user through a simple form, but I cannot get the user to be manually authenticated.
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
public function addAction($user)
{
$token =new UsernamePasswordToken(
$user->getUsername(),
$user->getPassword(),
'secured_area',
$user->getRoles()
);
$this->get('security.context')->setToken($token);
// as suggested in some other answers
$request->getSession()->set('_security_secured_area', serialize($token));
// as suggested in http://techblog.zabuchy.net/2012/manually-authenticate-symfony-2-user/
return $this->redirect($this->generateUrl('acme_project_secure_show' )
);
}
}
The redirection to the secure route works, but then the method $this->getUser()
returns null as the authentication is not set properly...
I can get the user from $user= $this->get('security.context')->getToken();
instead of $user= $this->get('security.context')->getToken()->getUser();
shortcut for $this->getUser()
see the Book here
Any idea why?