Question

i am trying to implement a redirect after login in my symfony2 application in order to redirect if my user has one attribute or not. I've created the class AuthenticationSuccessHandler.php inside Handler Folder in my project:

    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 {

        public function __construct( HttpUtils $httpUtils, array $options ) {
            parent::__construct( $httpUtils, $options );
        }

        public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {


        $user = $token->getUser();

        if($user->getProfile()!=1){
            $url = 'fos_user_profile_edit';
        }else{
            $url = 'My_route';
        }

        return new RedirectResponse($this->router->generate($url));
        }
    }

But when i log in, i get an error:

Notice: Undefined property: Me\MyBundle\Handler\AuthenticationSuccessHandler::$router in /var/www/MyBundle/src/Me/MyBundle/Handler/AuthenticationSuccessHandler.php line 28

The error is in happening in the "return new RedirectResponse($this->router->generate($url));"

I also have my service:

    my_auth_success_handler:
            class: Me\MyBundle\Handler\AuthenticationSuccessHandler
            public: false
            arguments: [ @security.http_utils, [] ]

and the success handler in the security.yml:

    fos_facebook:
            success_handler: my_auth_success_handler

Any ideas? Thank you very much.

Was it helpful?

Solution

You have no @router service injected. Modify your constructor

protected $router;
public function __construct( HttpUtils $httpUtils, array $options, $router ) {
    $this->router = $router;
    parent::__construct( $httpUtils, $options );
}

And service definition:

...
arguments: [ @security.http_utils, [], @router ]

OTHER TIPS

With Symfony >= 2.8, you have the AutowirePass that makes a simplification in service definition.

use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Http\HttpUtils;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler 
{

    /**
     * @var Router
     */
    protected $router;

    public function __construct(HttpUtils $httpUtils, array $options = [], Router $router) 
    {
        parent::__construct($httpUtils, $options);
        $this->router = $router;
    }

Note that the default value "$options = []" is important for the AutowirePass : without that, an exception will be thrown. But you have an empty array.

into services.yml:

 my_auth_success_handler:
        class: Me\MyBundle\Handler\AuthenticationSuccessHandler
        public: false
        autowire: true

No need to specify arguments here ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top