1
votes

I'm using FOSUserBundle together with NnmMultiUserBundle. When a user register, using my form_handler, the User entity is created, and should automatically login this new user.

I haven't been able to make that automatic login works.

My Controller:

public function registerAction()
    {
        $discriminator = $this->container->get('nmn_user_discriminator');
        $discriminator->setClass('Travelyo\UserBundle\Entity\UserFrontend');
        $form = $discriminator->getRegistrationForm();

        //$form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled, array(User::ROLE_CUSTOMER));
        if ($process) {
            $user = $form->getData();

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_frontend_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_frontend_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->container->get('templating')->renderResponse('TravelyoUserBundle:Registration:user_frontend.form.html.' . $this->getEngine(), array('form' => $form->createView(),));
    }

Athenticate method:

protected function authenticateUser(UserInterface $user)
    {
        try {
            $this->container->get('fos_user.user_checker')->checkPostAuth($user);
        } catch (AccountStatusException $e) {
            // Don't authenticate locked, disabled or expired users
            return;
        }

        $providerKey = $this->container->getParameter('fos_user.firewall_name');
        $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
        $this->container->get('security.context')->setToken($token);
    }

After calling athenticate the controller redirects to ConfirmedAction, where it tries to get the "logged in" user:

public function confirmedAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array(
        'user' => $user,
    ));
}

Error message:

Fatal error: Call to a member function getUser() on a non-object in /Data/Web/Local/travelyo/vendor/bundles/FOS/UserBundle/Controller/RegistrationController.php on line 115

If I then try to login using the login form it works fine (meaning that the creation of the user is working). But I can't understand why this token cannot be retrieved from security.context.

1
Accept some answers for your previous questionsVitalii Zurian
Ah in fact... I often mention the right answer in a comment of my main post or by editing the original question. I'll do so... Thx!LEM01
I did small search in my favorite questions - and found there similar one.Vitalii Zurian
Thanks, for the help! As you can seen I now accept answers ;-) And I've posted the solution below.LEM01
Good job! Gladly looking forward to your next questions ;)Vitalii Zurian

1 Answers

2
votes

Security context is (as described in symfony 2 doc) not shared across firewall.

My registration form was in the "public firewall" => /register.

When authenticating the user (right after the user is registered), the token is saved for the public security.context.

I was then redirected to my actual "account" firewall => /account, and no token was found in the security.context as the token had never been defined for that firewall.

In order to solve it I simply moved my registration process under the same firewall, and it now works like a charm.

But I would still assume it should be possible to set a token for another security.context, if anyone know how it might be useful.

Thanks!