4

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.

1 Answer 1

5

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

Yeahh!!! It's working, but it is redirecting me to the login page once authenticated (user/login) instead the previous one. I think it's because in the referer variable is set the login (wich is the last i've visited) Is there any way to fix this? Thanks :-) :-) :-)
Try $referer = $request->getSession()->get('_security.'.$this->providerKey.'.target_path', null);
Is getting null because is redirecting me to My_customurl i've been seeing this post but i am unable to get it working: stackoverflow.com/questions/16079592/…
Any ideas please? anyone?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.