2

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.

Community
  • 1
  • 1
Harry
  • 21
  • 1
  • 2

1 Answers1

4

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

Matt
  • 10,633
  • 3
  • 46
  • 49