Skip to content

Instantly share code, notes, and snippets.

@simonjodet
Created October 21, 2012 16:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save simonjodet/3927516 to your computer and use it in GitHub Desktop.
Save simonjodet/3927516 to your computer and use it in GitHub Desktop.
Silex automatic post-registration user authentication
<?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('/');
}
);
@PYuen1029
Copy link

Thanks a bunch for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment