How to automatically login a new user after registration with a custom user provider in Silex?

∥☆過路亽.° 提交于 2019-12-05 02:07:58

问题


Context : I created a custom user provider for my Silex Application and I can now register and log my own user perfectly. However, I need now to automatically log in my user after registration and this doesn't work.

Here is my security section :

'secured' => array(
        'pattern' => '^/',
        'anonymous' => false,
        'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
        'logout' => array('logout_path' => '/logout', 'invalidate_session' => true),
        'users' => $app->share(function () use ($app) {
            return new Partner\DAO\PartnerDAO($app['db']);
        })
    )

Here is my userProviderInterface :

public function refreshUser(UserInterface $user)
{
    if (!$user instanceof Partner) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }

    return $this->loadUserByUsername($user->getUsername());
}


public function supportsClass($class)
{
    return 'Partner\Entity\Partner' === $class;
}

With Xdebug, I can see that here :

  foreach ($this->userProviders as $provider) {
        try {
            $refreshedUser = $provider->refreshUser($user);
            $token->setUser($refreshedUser);

            if (null !== $this->logger) {
                $this->logger->debug('User was reloaded from a user provider.', array('username' => $refreshedUser->getUsername(), 'provider' => get_class($provider)));
            }

            return $token;

$this->userProviders only contains one provider : 'Users' => null

Note that :

Login, register and logout work perfectly. This error is only triggered when I want to automatically login my user after registration. Here is the code :

$token = new UsernamePasswordToken($partner, null, 'secured', $partner->getRoles());
// Note that $partner->getRoles()  is array('ROLE_USER')
$app['security.token_storage']->setToken($token);
return $app->redirect($app["url_generator"]->generate("partner_home"));

After registration, I automatically create the token and redirect the user in the secured area but Silex redirect it on the login page and if I click somewhere i get :

There is no user provider for user "Partner\Entity\Partner".

I don't know where to search anymore and I could really need some help.


回答1:


Founded a solution, more like a workaround, but it make the job :

Instead of using the security/token method, I now log the user by generating a request to the login_check controller in order to generate all necessary login event.

In my controller :

$subRequest = Request::create(
$app['url_generator']->generate('login_check'),
    'POST',
    array(
        '_username' => $partner->getEmail(),
        '_password' => $rawPassword,
        $request->cookies->all(),
        array(),
        $request->server->all()
    )
);
$app->handle($subRequest, HttpKernelInterface::MASTER_REQUEST, false);

Wich require "'require_previous_session' => false" in your firewall configuration :

'secured' => array(
        'pattern' => '^/',
        'anonymous' => false,
        'form' => array(
            'login_path' => '/login',
            'check_path' => '/login_check',
            'require_previous_session' => false
        ),
        'logout' => array('logout_path' => '/logout', 'invalidate_session' => true),
        'users' => $app->share(function () use ($app) {
            return new Partner\DAO\PartnerDAO($app['db']);
        })
    )

I'm not sure it's a good or a bad solution, but it's working.



来源:https://stackoverflow.com/questions/32744298/how-to-automatically-login-a-new-user-after-registration-with-a-custom-user-prov

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!