I am trying to implement a method to force users to change their default password on their first login in my Symfony application.
At the moment I have set up an event listener to listen for the InteractiveLogin event.
namespace App\EventListener;
use App\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class LoginListener
{
private $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
// Get the User entity.
$user = $event->getAuthenticationToken()->getUser();
if ($user->getForcepasswordchange()) {
return new RedirectResponse($this->urlGenerator->generate('force_password_change'));
}
}
}
It basically checks for a boolean flag in the user entity that is set to true for new users. It picks up the user and the method gets to the RedirectResponse line but it just ends up going to the homepage (the default login behaviour).
I am not sure how to force it to not continue the login process and redirect to my password change page.