Created
October 21, 2012 16:35
-
-
Save simonjodet/3927516 to your computer and use it in GitHub Desktop.
Silex automatic post-registration user authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app['security.firewalls'] = array( | |
'user_firewall' => array( | |
'pattern' => new \Application\UserRequestMatcher($app['request']), | |
'form' => array('login_path' => '/login', 'check_path' => '/authenticate'), | |
'logout' => array('logout_path' => '/logout'), | |
'users' => $app->share(function () use ($app) | |
{ | |
return new \Application\UserProvider($app); | |
}), | |
), | |
); | |
//... | |
$app->post( | |
'/register', | |
function (\Silex\Application $app) | |
{ | |
$form->bind($app['request']); | |
$data = $form->getData(); | |
$UserModel = new \Application\UserModel($app); | |
$UserModel->create($data); | |
$User = new \Symfony\Component\Security\Core\User\User($data['username'], $data['password'], array('ROLE_USER')); | |
$app['security']->setToken(new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($User, $User->getPassword(), 'user_firewall', array('ROLE_USER'))); | |
return $app->redirect('/'); | |
} | |
); |
thank you!
This way works for Symfony 2.6.x - Symfony 3.0.x
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));
}
}
Thanks a bunch for this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks so much for this, I have been looking everywhere trying to find out how to do this