title: Overriding FOSUserBundle loginAction() to redirect to custom routes
body: <p>I am working on a Symfony 3.3.8 project with FOSUserBundle. I have created two registration and two profile pages for student and provider respectively. Now I am trying to override the FOSUserBundle's loginAction() method, where I am checking the logged-in user for it's role. If the role is <code>ROLE_STUDENT</code> I am trying to redirect the user to student profile page after successful login and if the role is <code>ROLE_PROVIDER</code> I want the user to be redirected to the provider profile page after successful login. Here is my overridden SecurityController with <code>loginAction()</code>:</p>
  
  <pre><code>&lt;?php
  
  namespace Epita\HousingBundle\Controller;
  
  use FOS\UserBundle\Controller\SecurityController as BaseController;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\Security\Core\Security;
  use Symfony\Component\Security\Core\SecurityContextInterface;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Component\Security\Core\Exception\AuthenticationException;
  
  class SecurityController extends BaseController {
  
      /**
       * 
       */
      public function loginAction(Request $request) {
           /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
          $session = $request-&gt;getSession();
          $key = '_security.main.target_path';
  
          $securityContext = $this-&gt;container-&gt;get('security.authorization_checker');
          if ($securityContext-&gt;isGranted('IS_AUTHENTICATED_REMEMBERED')) {
              $user = $this-&gt;getUser();            
          }
  
          if ($this-&gt;container-&gt;get('session')-&gt;has($key)) 
          {
             if ($this-&gt;get('security.authorization_checker')-&gt;isGranted('ROLE_STUDENT')) {
                  return $this-&gt;redirectToRoute('student_profile');
               } else if ($this-&gt;get('security.authorization_checker')-&gt;isGranted('ROLE_PROVIDER')) {
                  return $this-&gt;redirectToRoute('provider_profile');
               } 
          }   
          else{
              return $this-&gt;redirectToRoute('student_profile');
          }
  
          if (class_exists('\Symfony\Component\Security\Core\Security')) {
              $authErrorKey = Security::AUTHENTICATION_ERROR;
              $lastUsernameKey = Security::LAST_USERNAME;
          } else {
              // BC for SF &lt; 2.6
              $authErrorKey = SecurityContextInterface::AUTHENTICATION_ERROR;
              $lastUsernameKey = SecurityContextInterface::LAST_USERNAME;
          }
  
          // get the error if any (works with forward and redirect -- see below)
          if ($request-&gt;attributes-&gt;has($authErrorKey)) {
              $error = $request-&gt;attributes-&gt;get($authErrorKey);
          } elseif (null !== $session &amp;&amp; $session-&gt;has($authErrorKey)) {
              $error = $session-&gt;get($authErrorKey);
              $session-&gt;remove($authErrorKey);
          } else {
              $error = null;
          }
  
          if (!$error instanceof AuthenticationException) {
              $error = null; // The value does not come from the security component.
          }
  
          // last username entered by the user
          $lastUsername = (null === $session) ? '' : $session-&gt;get($lastUsernameKey);
  
          if ($this-&gt;has('security.csrf.token_manager')) {
              $csrfToken = $this-&gt;get('security.csrf.token_manager')-&gt;getToken('authenticate')-&gt;getValue();
          } else {
              // BC for SF &lt; 2.4
              $csrfToken = $this-&gt;has('form.csrf_provider')
                  ? $this-&gt;get('form.csrf_provider')-&gt;generateCsrfToken('authenticate')
                  : null;
          }
  
          return $this-&gt;renderLogin(array(
              'last_username' =&gt; $lastUsername,
              'error' =&gt; $error,
              'csrf_token' =&gt; $csrfToken,
          ));
  
      }
  
      /**
       *
       * @param array $data
       *
       * @return \Symfony\Component\HttpFoundation\Response
       */
      protected function renderLogin(array $data)
      { 
          $template = sprintf('EpitaHousingBundle:Security:login.html.twig');
  
          return $this-&gt;container-&gt;get('templating')-&gt;renderResponse($template, $data);
  
      }
  
      public function checkAction()
      {
          throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
      }
  
      public function logoutAction()
      {
          throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
      }
  }
  </code></pre>
  
  <p>But this solution does not work for me. My guess is I have to play around with the session. Since I am new to this can anyone help me?</p>
  
  <p>Thanks in advance. Let me know if you need more details.</p>