在Silex的注册后自动登录 [英] Automatic login after registration in Silex
问题描述
我要自动登录用户注册后。首先我想这种方式
https://gist.github.com/simonjodet/3927516 。卜山雀没有工作。我发现别人有同样的问题,发现<一个一个解决办法href=\"http://stackoverflow.com/questions/32744298/how-to-automatically-login-a-new-user-after-registration-with-a-custom-user-prov\">this问题,但它不为我工作。
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.
这是我的code,在登录过程中正常工作。但是,并非注册时自动登录。
This is my code, the login process works correctly. But not login automatically when registering.
我的安全配置:
$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'];
}
)
);
在注册我的UserController.php code:
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);
我使用的用户电子邮件,因为它应该是什么样的用户提供用户名。
它没有登录,这是独白的输出。用户名是NONE_PROVIDED,而应该是用户的电子邮件。
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 [] []
编辑:
尽管问,我发现,也许问题是,当我让我从'localhost'的,而不是发送它的自动请求local.mysite.com,如何解决?
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?
推荐答案
我会选择的要点,而不是重定向,这似乎给的hackish我。
I would opt for the gist instead of the redirect, this seems hackish to me.
有关新Symfony的安全组件,您需要调整code一点点。从所以这个答案:
For new Symfony Security component you need to adapt the code a little bit. From SO this answer:
<?php
// on your user registration controller...
// create and store an authenticated token
$token = new UsernamePasswordToken(
$user,
$user->getPassword(),
'main', //key of the firewall you are trying to authenticate
$user->getRoles()
);
$app['security.token_storage']->setToken($token);
// _security_main is, again, the key of the firewall
$app['session']->set('_security_main', serialize($token));
$app['session']->save(); // this will be done automatically but it does not hurt to do it explicitly
return $app->redirect('/where-ever-you-want');
我没有测试此code!
I haven't tested this code!
更新
作为参考,Symfony的文档有一个很好的例子:如何模拟验证了令牌在功能测试
这篇关于在Silex的注册后自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!