I want to automatically login user after registering. First I tried this way https://gist.github.com/simonjodet/3927516. Bu tit did not work. I found out someone else had the same problem and found a workaround in this question, but it does not work for me.
This is my code, the login process works correctly. But not login automatically when registering.
My security configuration:
$app['security.firewalls'] = array(
'main' => array(
'pattern' => '^(/user|/logout|/login_check)',
'form' => array(
'login_path' => '/login/',
'check_path' => '/login_check/',
'require_previous_session' => false,
'username_parameter'=> 'form[email]',
'password_parameter' => 'form[password]'
),
'remember_me' => array(
'key' => 'adfafasfasdfdasfasdfa', //whatever random string
'remember_me_parameter' => 'form[remember]'
),
'logout' => array('logout_path' => '/logout/', 'invalidate_session' => true),
'users' => function(Silex\Application $app){
return $app['user_manager'];
}
)
);
My UserController.php code on signing up:
if ($signupForm->isValid()) {
$plainPassword = $data['password'];
//preparing data to be inserted
$password = self::encodePassword($data['email'], $plainPassword, $app['security.encoder.digest']);
//inserting the user
$userManager->insertUser($data, $password, $app['slugger']);
//generate a request to login_check
$subRequest = \Symfony\Component\HttpFoundation\Request::create(
'/login_check/',
'POST',
array(
'form[email]' => $data['email'],
'form[password]' => $plainPassword,
$app['request']->cookies->all(),
array(),
$app['request']->server->all()
));
$app->handle($subRequest, \Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST, false);
I am using the user email as what it should be user provider 'username'. It does not login, and this is Monolog's output. Username is 'NONE_PROVIDED' while it should be the user's email.
[2016-03-16 19:29:41] myapp.INFO: Matched route "POST_login_". {"route_parameters":{"_controller":"user.controller:login","_route":"POST_login_"},"request_uri":"http://local.mysite.com/login/"} []
[2016-03-16 19:29:41] myapp.INFO: > POST /login/ [] []
[2016-03-16 19:29:41] myapp.INFO: Matched route "POST_login_check_". {"route_parameters":{"_controller":"loginCheck","_route":"POST_login_check_"},"request_uri":"http://localhost/login_check/"} []
[2016-03-16 19:29:41] myapp.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at /home/myuser/www/local.mysite.com/vendor/symfony/security/Core/Authentication/Provider/UserAuthenticationProvider.php:73, Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): Username \"NONE_PROVIDED\" does not exist. at /home/myuser/www/local.mysite.com/app/Model/Manager/UserManager.php:58)"} []
[2016-03-16 19:29:41] myapp.DEBUG: Authentication failure, redirect triggered. {"failure_path":"/login/"} []
[2016-03-16 19:29:41] myapp.INFO: < 302 http://localhost/login/ [] []
[2016-03-16 19:29:41] myapp.INFO: < 200 [] []
EDIT:
While asking, I found out that maybe the problem is that when I make the automatic request I am sending it from 'localhost' instead of 'local.mysite.com', how to fix that?
I would opt for the gist instead of the redirect, this seems hackish to me.
For new Symfony Security component you need to adapt the code a little bit. From SO this answer:
I haven't tested this code!
UPDATE For reference, Symfony documentation has a good example of this: How to Simulate Authentication with a Token in a Functional Test