Redirect if the user is logged in

后端 未结 2 1982
既然无缘
既然无缘 2020-12-23 15:25

I am building a web application with Symfony 2, using the FOSUserBundle bundle.
Users create an account, login and start using the application.

What I want to

2条回答
  •  粉色の甜心
    2020-12-23 16:04

    You can also do this solution if you want to just check once:

    There is an event triggered each time there's a successful login being done. The event is named:

    security.interactive_login

    In order to subscribe to this event, you have to create a service container with your created class let's say "LoginListener.php" and inject the tag "kernel.even_listener" with the event "security.interactive_login":

    
       
    
    

    in LoginListener:

    use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
    
    public function onLoginSuccess(InteractiveLoginEvent $event) {
       if ($this->_security->isGranted('IS_AUTHENTICATED_FULLY')) {
          //your code here...
       }
    }
    

    you can also add other dependencies and inject it to the constructor, in my case I had to inject security, session and container:

    public function __construct(SecurityContext $security, Session $session,            ContainerInterface $container) {
    
    }
    

提交回复
热议问题