title: Symfony 2 FosUser: Authenticate after registration
body: <p>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.</p>
  
  <p>I haven't been able to make that automatic login works.</p>
  
  <p><strong>My Controller:</strong></p>
  
  <pre><code>public function registerAction()
      {
          $discriminator = $this-&gt;container-&gt;get('nmn_user_discriminator');
          $discriminator-&gt;setClass('Travelyo\UserBundle\Entity\UserFrontend');
          $form = $discriminator-&gt;getRegistrationForm();
  
          //$form = $this-&gt;container-&gt;get('fos_user.registration.form');
          $formHandler = $this-&gt;container-&gt;get('fos_user.registration.form.handler');
          $confirmationEnabled = $this-&gt;container-&gt;getParameter('fos_user.registration.confirmation.enabled');
  
          $process = $formHandler-&gt;process($confirmationEnabled, array(User::ROLE_CUSTOMER));
          if ($process) {
              $user = $form-&gt;getData();
  
              if ($confirmationEnabled) {
                  $this-&gt;container-&gt;get('session')-&gt;set('fos_user_send_confirmation_email/email', $user-&gt;getEmail());
                  $route = 'fos_frontend_registration_check_email';
              } else {
                  $this-&gt;authenticateUser($user);
                  $route = 'fos_frontend_registration_confirmed';
              }
  
              $this-&gt;setFlash('fos_user_success', 'registration.flash.user_created');
              $url = $this-&gt;container-&gt;get('router')-&gt;generate($route);
  
              return new RedirectResponse($url);
          }
  
          return $this-&gt;container-&gt;get('templating')-&gt;renderResponse('TravelyoUserBundle:Registration:user_frontend.form.html.' . $this-&gt;getEngine(), array('form' =&gt; $form-&gt;createView(),));
      }
  </code></pre>
  
  <p><strong>Athenticate method:</strong>     </p>
  
  <pre><code>protected function authenticateUser(UserInterface $user)
      {
          try {
              $this-&gt;container-&gt;get('fos_user.user_checker')-&gt;checkPostAuth($user);
          } catch (AccountStatusException $e) {
              // Don't authenticate locked, disabled or expired users
              return;
          }
  
          $providerKey = $this-&gt;container-&gt;getParameter('fos_user.firewall_name');
          $token = new UsernamePasswordToken($user, null, $providerKey, $user-&gt;getRoles());
          $this-&gt;container-&gt;get('security.context')-&gt;setToken($token);
      }
  </code></pre>
  
  <p>After calling athenticate the controller redirects to ConfirmedAction, where it tries to get the "logged in" user:</p>
  
  <pre><code>public function confirmedAction()
  {
      $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
      if (!is_object($user) || !$user instanceof UserInterface) {
          throw new AccessDeniedException('This user does not have access to this section.');
      }
  
      return $this-&gt;container-&gt;get('templating')-&gt;renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this-&gt;getEngine(), array(
          'user' =&gt; $user,
      ));
  }
  </code></pre>
  
  <p><strong>Error message:</strong></p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>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.</p>