title: AuthenticationSuccessHandler, redirect after login and referrer
body: <p>I've just added with this forum's help an AuthenticationSuccessHandler which implements on my site a redirection when the user login via fosuserbundle or fosfacebookbundle. The redirection changes when the user has the profile completed or not, and if it has it completed, I want them to be redirected where they were previously, this is why I am using the referrer variable.</p>
  <pre>
      namespace Me\MyBundle\Handler;
      
      use Symfony\Component\Security\Http\HttpUtils;
      use Symfony\Component\HttpFoundation\RedirectResponse;
      use Symfony\Component\HttpFoundation\Request;
      use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
      use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
      
      
      class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {
      
          protected $router;
      
          public function __construct( HttpUtils $httpUtils, array $options, $router) {
          $this->router = $router;
              parent::__construct( $httpUtils, $options );
          }
      
          public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
              
          $user = $token->getUser();
          
          if($user->getProfileComplete()!=1){
      //redirects to profile editing
              $url = 'fos_user_profile_edit';
          }else{
              $referer = $request->headers->get('referer');
              if($referer == NULL){
      //redirects to custom url if there is not any referer
                  $url = 'My_customurl';
              }else{
      //redirects to the referer
                  $url = $referer;
              }
          }
      
          return new RedirectResponse($this->router->generate($url));
          }
      }
  
  </pre> 
  <p>my security.yml note that I have the use_referrer declared in both fos_facebook and form_login:</p>
  <pre>
  firewalls:
          main:
              pattern: ^/
              fos_facebook:
                app_url: "http://apps.facebook.com/app"
                server_url: "http://www.app.com"
                login_path: /user/login
                check_path: /facebook/login_check
                provider: fos_facebook_provider
                success_handler: my_auth_success_handler
                use_referer: true
              form_login:
                  provider: fos_userbundle
                  csrf_provider: form.csrf_provider
                  login_path: /user/login
                  check_path: /user/login_check
                  use_forward: false
                  success_handler: my_auth_success_handler
                  use_referer: true
                  failure_path: null
              logout:
                  path: /user/logout
              anonymous: ~
              remember_me:
                    key:      mySuperKey
                    lifetime: 4147200
                    path:     /
                    domain:   ~
  </pre>
  <p>When I login and I don't have my profile completed, I am getting redirected successfully but when it is completed and referrer is not null I am having 500 errors:</p>
  <p>When I login with Facebook I get an 500 error: &quot;Unable to generate a URL for the named route &quot;http://app.com/app_dev.php/user/login&quot; as such route does not exist.&quot; and the url in firefox is this: &quot;http://app.com/app_dev.php/facebook/login_check&quot;</p>
  <p>And when I login with the form, I get a 500 error: &quot;Unable to generate a URL for the named route &quot;http://app.com/app_dev.php/user/login&quot; as such route does not exist.&quot; and the url in firefox is this: &quot;http://app.com/app_dev.php/user/login_check&quot;</p>
  <p>Any ideas, I am getting crazy. I thing its because the referral has stored user/login and when it go to login_check goes to user/login again.</p>