Symfony – AuthenticationSuccessHandler, redirect after login and referrer


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.

    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();
    //redirects to profile editing
            $url = 'fos_user_profile_edit';
            $referer = $request->headers->get('referer');
            if($referer == NULL){
    //redirects to custom url if there is not any referer
                $url = 'My_customurl';
    //redirects to the referer
                $url = $referer;
        return new RedirectResponse($this->router->generate($url));

my security.yml note that I have the use_referrer declared in both fos_facebook and form_login:

            pattern: ^/
              app_url: ""
              server_url: ""
              login_path: /user/login
              check_path: /facebook/login_check
              provider: fos_facebook_provider
              success_handler: my_auth_success_handler
              use_referer: true
                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
                path: /user/logout
            anonymous: ~
                  key:      mySuperKey
                  lifetime: 4147200
                  path:     /
                  domain:   ~

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:

When I login with Facebook I get an 500 error: "Unable to generate a URL for the named route "" as such route does not exist." and the url in firefox is this: ""

And when I login with the form, I get a 500 error: "Unable to generate a URL for the named route "" as such route does not exist." and the url in firefox is this: ""

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.

Best Solution

You're trying generate route using actual url, not route name. Try this code:

if ($user->getProfileComplete() != 1) {
    $url = $this->router->generate('fos_user_profile_edit');
} else {
    $referer = $request->headers->get('referer');
    if ($referer == NULL) {
        $url = $this->router->generate('My_customurl');
    } else {
        $url = $referer;

return new RedirectResponse($url);
Related Question