Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LogIn in Controller with FOSUserBundle

I'm working on a project where User can log in with a certificate.

So if there is a valid certificate in a POST request I want to login the user in a controller and redirect to the home site.

I'm using the FOSUserBundle but did not found anything that explains whether this is possible or how it's done.

Did anybody already did this or has a idea how it could be done?

like image 987
Johannes Klauß Avatar asked Mar 10 '26 23:03

Johannes Klauß


1 Answers

I finally found the answer.

    $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($user, $pw, "main", array("ROLE_USER"));
    $this->get('security.context')->setToken($token);

    $event = new \Symfony\Component\Security\Http\Event\InteractiveLoginEvent($this->getRequest(), $token);
    $this->get('event_dispatcher')->dispatch('security.interactive_login', $event);

    $user = $this->get('security.context')->getToken()->getUser();
like image 168
Johannes Klauß Avatar answered Mar 13 '26 18:03

Johannes Klauß