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(); 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)); } }
my security.yml note that I have the use_referrer declared in both fos_facebook and form_login:
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: ~
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 "http://app.com/app_dev.php/user/login" as such route does not exist." and the url in firefox is this: "http://app.com/app_dev.php/facebook/login_check"
And when I login with the form, I get a 500 error: "Unable to generate a URL for the named route "http://app.com/app_dev.php/user/login" as such route does not exist." and the url in firefox is this: "http://app.com/app_dev.php/user/login_check"
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.
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);