title: Symfony2 auto-login after registration with FOSUser Bundle
body: <p>I'm trying to do an auto login with symfony2 and FOSUserBundle After Registeration.
  I want to make like When User register then go to next page (ProfileController-editAction()) and fill some User Form and then Finish.</p>
  
  <p>I had made this but we required every page password for User Profile Edit after register and I want this without password How I can do this ?</p>
  
  <p>I faced this problem because profile edit action work only When User login</p>
  
  <p>Security.yml</p>
  
  <pre><code>parameters:
      security.acl.permission.map.class:   Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap
  security:
      encoders:
          FOS\UserBundle\Model\UserInterface: sha512
  
  role_hierarchy:
      ROLE_ADMIN:       ROLE_USER
      ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
      ROLE_ADMIN_GUEST: [ROLE_SONATA_ADMIN_MTCATEGORY_GUEST, ROLE_SONATA_ADMIN_MTBRAND_GUEST, ROLE_SONATA_ADMIN_MTSERIES_GUEST]
      SONATA:
          - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are not using acl then this line must be uncommented
  
  providers:
      fos_userbundle:
          id: fos_user.user_provider.username_email
  
      my_fos_facebook_provider:
          id: my.facebook.user   # see "Example Custom User Provider using the FOS\UserBundle" chapter further down
  
  firewalls:
      admin:
          pattern:      /admin(.*)
          form_login:
              provider:       fos_userbundle
              login_path:     /admin/login
              use_forward:    false
              check_path:     /admin/login_check
              failure_path:   null
          logout:
              path:           /admin/logout
          anonymous:    true
  
      main:
          pattern:      .*
          form_login:
              success_handler: security.authentication.customized_success_handler
              failure_handler: security.authentication.customized_success_handler
              provider:       fos_userbundle
              login_path:     /login
              use_forward:    false
              check_path:     /login_check
              failure_path:   null
          oauth:
              resource_owners:
                  facebook:    /login/check-facebook
                  twitter:     /login/check-twitter
                  google:      /login/check-google
              login_path:        /login
              failure_path:      /login
              default_target_path: /profile
              oauth_user_provider: 
                  service: my_user_provider
  
          fos_facebook:
              app_url: "http://apps.facebook.com/appName/"
              server_url: "http://localhost/facebookApp/"
              login_path: /login
              check_path: /login_fb_check
              default_target_path: /
              provider: my_fos_facebook_provider
          logout:
              invalidate_session: false
          anonymous:    true
  
  access_control:
      - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ADMIN_GUEST] }
      - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  
  acl:
      connection: default
  </code></pre>
  
  <p>RegisterationController.php</p>
  
  <pre><code> &lt;?php
   namespace XXX\UserBundle\Controller;
  
  use Symfony\Component\DependencyInjection\ContainerAware;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  use Symfony\Component\Security\Core\Exception\AccountStatusException;
  use FOS\UserBundle\Model\UserInterface;
  
  /**
   * Controller managing the registration
   *
   * @author Siddharth Singh &lt;Siddharth.find@gmail.com&gt;
   */
  class RegistrationController extends ContainerAware
  {
  public function registerAction(Request $request)
  {
      $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);
      if ($request-&gt;isXmlHttpRequest()) {
    //  echo 'test-&gt;',$process;
          if ($process) {
              $user = $form-&gt;getData();
              $authUser = false;
              if ($confirmationEnabled) {
                  $this-&gt;container-&gt;get('session')-&gt;set('fos_user_send_confirmation_email/email', $user-&gt;getEmail());
                  $result = array('success' =&gt; false);
              } else {
                  $authUser = true;
                  $result = array('success' =&gt; true);
              }
              $response = new Response(json_encode($result));
              $response-&gt;headers-&gt;set('Content-Type', 'application/json');
              //return $response;
              if ($authUser) {
                  $this-&gt;authenticateUser($user, $response);
              }
              return $response;
          }
          $result = array('success' =&gt; false);
          $response = new Response(json_encode($result));
          $response-&gt;headers-&gt;set('Content-Type', 'application/json');
          return $response;
      }
  
      return $this-&gt;container-&gt;get('templating')-&gt;renderResponse('XXXUserBundle:Registration:register.html.'.$this-&gt;getEngine(), array(
          'form' =&gt; $form-&gt;createView(),
      ));
  }
  
  /**
   * Tell the user to check his email provider
   */
  public function checkEmailAction()
  {
      $email = $this-&gt;container-&gt;get('session')-&gt;get('fos_user_send_confirmation_email/email');
      $this-&gt;container-&gt;get('session')-&gt;remove('fos_user_send_confirmation_email/email');
      $user = $this-&gt;container-&gt;get('fos_user.user_manager')-&gt;findUserByEmail($email);
  
      if (null === $user) {
          throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
      }
  
      return $this-&gt;container-&gt;get('templating')-&gt;renderResponse('XXXUserBundle:Registration:checkEmail.html.'.$this-&gt;getEngine(), array(
          'user' =&gt; $user,
      ));
  }
  
  /**
   * Receive the confirmation token from user email provider, login the user
   */
  public function confirmAction($token)
  {
      $user = $this-&gt;container-&gt;get('fos_user.user_manager')-&gt;findUserByConfirmationToken($token);
  
      if (null === $user) {
          throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
      }
  
      $user-&gt;setConfirmationToken(null);
      $user-&gt;setEnabled(true);
      $user-&gt;setLastLogin(new \DateTime());
  
      $this-&gt;container-&gt;get('fos_user.user_manager')-&gt;updateUser($user);
      $response = new RedirectResponse($this-&gt;container-&gt;get('router')-&gt;generate('fos_user_registration_confirmed'));
      $this-&gt;authenticateUser($user, $response);
  
      return $response;
  }
  
  /**
   * Tell the user his account is now confirmed
   */
  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('XXXUserBundle:Registration:confirmed.html.'.$this-&gt;getEngine(), array(
          'user' =&gt; $user,
      ));
  }
  
  /**
   * Authenticate a user with Symfony Security
   *
   * @param \FOS\UserBundle\Model\UserInterface        $user
   * @param \Symfony\Component\HttpFoundation\Response $response
   */
  protected function authenticateUser(UserInterface $user, Response $response)
  {
      try {
          $this-&gt;container-&gt;get('fos_user.security.login_manager')-&gt;loginUser(
              $this-&gt;container-&gt;getParameter('fos_user.firewall_name'),
              $user,
              $response);
      } catch (AccountStatusException $ex) {
          // We simply do not authenticate users which do not pass the user
          // checker (not enabled, expired, etc.).
      }
  }
  
  /**
   * @param string $action
   * @param string $value
   */
  protected function setFlash($action, $value)
  {
      $this-&gt;container-&gt;get('session')-&gt;getFlashBag()-&gt;set($action, $value);
  }
  
  protected function getEngine()
  {
      return $this-&gt;container-&gt;getParameter('fos_user.template.engine');
  }
  }
  </code></pre>
  
  <p>ProfileController.php</p>
  
  <pre><code>&lt;?php
  namespace XXX\UserBundle\Controller;
  
  use Symfony\Component\DependencyInjection\ContainerAware;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  use FOS\UserBundle\Model\UserInterface;
  use Hip\MandrillBundle\Message;
  use Hip\MandrillBundle\Dispatcher;
  
  /**
   * Controller managing the user profile
   *
   * @author Siddharth Singh &lt;siddharth.find@gmail.com&gt;
   */
  class ProfileController extends ContainerAware
  {
  /**
   * Show the user
   */
  public function showAction()
  {
      $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('XXXUserBundle:Profile:show.html.'.$this-&gt;container-&gt;getParameter('fos_user.template.engine'), array('user' =&gt; $user));
  }
  
  /**
   * Edit the user
   */
  public function editAction()
  {
      $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.');
      }
  
      $form = $this-&gt;container-&gt;get('fos_user.profile.form');
      $formHandler = $this-&gt;container-&gt;get('fos_user.profile.form.handler');
  
      $process = $formHandler-&gt;process($user);
      if ($process) {
          $this-&gt;setFlash('fos_user_success', 'profile.flash.updated');
          $route = 'fos_user_profile_pro';
          $url = $this-&gt;container-&gt;get('router')-&gt;generate($route);
          $response = new RedirectResponse($url);
          return $response;
      }
  
      return $this-&gt;container-&gt;get('templating')-&gt;renderResponse(
          'XXXUserBundle:Profile:edit.html.'.$this-&gt;container-&gt;getParameter('fos_user.template.engine'),
          array('form' =&gt; $form-&gt;createView())
      );
  }
  
  /**
   * Complete the user profile
   */
  public function proAction()
  {
      $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.');
      }
  
      $form = $this-&gt;container-&gt;get('fos_user.profile.form');
      $formHandler = $this-&gt;container-&gt;get('fos_user.profile.form.handler');
  
      $process = $formHandler-&gt;process($user);
      if ($process) {
          $this-&gt;setFlash('fos_user_success', 'profile.flash.updated');
  
          return new RedirectResponse($this-&gt;getRedirectionUrl($user));
      }
  
      return $this-&gt;container-&gt;get('templating')-&gt;renderResponse(
          'XXXUserBundle:Profile:profile.html.'.$this-&gt;container-&gt;getParameter('fos_user.template.engine'),
          array('form' =&gt; $form-&gt;createView())
      );
  }
  
  /**
   * Generate the redirection url when editing is completed.
   *
   * @param \FOS\UserBundle\Model\UserInterface $user
   *
   * @return string
   */
  protected function getRedirectionUrl(UserInterface $user)
  {
      return $this-&gt;container-&gt;get('router')-&gt;generate('fos_user_profile_show');
  }
  
  /**
   * @param string $action
   * @param string $value
   */
  protected function setFlash($action, $value)
  {
      $this-&gt;container-&gt;get('session')-&gt;getFlashBag()-&gt;set($action, $value);
  }
  }
  </code></pre>