title: Redirect logged in users to home page when trying to access login page
body: <p>I am trying to make my Symfony2 website redirect logged in users to another page when trying to access the login page.</p>
  
  <p>This is my security.yml:</p>
  
  <pre><code>security:
      encoders:
          Cocoa\LoginBundle\Entity\User: 
          algorithm:        sha512
  
  role_hierarchy:
      ROLE_ADMIN:       ROLE_USER
      ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
  
  providers:
      administrators:
          entity: { class: CocoaLoginBundle:User, property: username }
  
  firewalls:
      admin_area:
          pattern:    ^/administration
          form_login:
              login_path:  login
              check_path:  login_check
          logout:
              path:   /administration/logout
              invalidate_session: true
              target: /
  
  
  access_control:
      - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
      - { path: ^/administration, roles: ROLE_ADMIN }
  </code></pre>
  
  <p>And this is my controller:</p>
  
  <pre><code>class LoginController extends Controller {
  
    public function loginAction() {
      if (false === $this-&gt;get('security.context')-&gt;isGranted('IS_AUTHENTICATED_FULLY')) {
        return $this-&gt;redirect($this-&gt;generateUrl('public_homepage'));
      }
  
      $request = $this-&gt;getRequest();
      $session = $request-&gt;getSession();
  
      if ($request-&gt;attributes-&gt;has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request-&gt;attributes-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
      } else {
        $error = $session-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
        $session-&gt;remove(SecurityContext::AUTHENTICATION_ERROR);
      }
  
      return $this-&gt;render('CocoaLoginBundle:Login:login.html.twig', array(
              'last_username' =&gt; $session-&gt;get(SecurityContext::LAST_USERNAME),
              'error' =&gt; $error));
    }
  
    public function loginCheckAction() {
  
    }
  
  }
  </code></pre>
  
  <p>If I access the login page as anonymous user I do see the login form. But after I log in and load the login page I get a 500 server error:</p>
  
  <pre><code>The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL. 
  </code></pre>
  
  <p>A proposed fix I read in another topic is to put the login page behind a firewall. I did that but still error 500.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks!</p>