Symfony2: How to log user manually? [duplicate]
Possible Duplicate:
Automatic post-registration user authentication
I want to log in the user after registration. I can do this by sending POST request with username and password by javascript to /login_check. At least I think it might succeed. But better solution would be achieving it by php code in controller, but I don't know how to do this using Symfony2.
If I remember correctly, the trick is to set the token directly into the security.context
service. Since you are using a username/password pair, you could instantiate a UsernamePasswordToken
class and set it in the security context.
$securityContext = $this->get('security.context');
$token = new UsernamePasswordToken($username, $password, $providerKey, $roles);
$securityContext->setToken($token);
For the $user
, $password
and $roles
parameters, it's obvious. For the $providerKey
, I must admit I don't know what to put in this one exactly. So, maybe getting the token from an AuthenticationProvider
could be easier, or if maybe you already have access to a TokenInterface
object.
The main point of my anwser is to set the token directly into the SecurityContext
object via the method setToken
. But I'm not sure exactly how to retrieve or create the token :)
Hope this help.
Regards,
Matt
精彩评论