I'm trying to implement your bundle, but when I follow your instruction... the part where I get the mail is ok, but not the part to reset the password.
I don't understand the problem the UserManager type is not found.
private $userManager;
public function __construct(MailerInterface $mailer, Environment $twig, UserPasswordHasherInterface $passwordHasher, UserManager $userManager)
{
$this->mailer = $mailer;
$this->twig = $twig;
$this->passwordHasher = $passwordHasher;
$this->userManager = $userManager;
}
public static function getSubscribedEvents()
{
return [
// Symfony 4.3 and inferior, use 'coop_tilleuls_forgot_password.create_token' event name
CreateTokenEvent::class => 'onCreateToken',
UpdatePasswordEvent::class => 'onUpdatePassword',
];
}
public function onCreateToken(CreateTokenEvent $event)
{
$passwordToken = $event->getPasswordToken();
$user = $passwordToken->getUser();
$message = (new Email())
->from('[email protected]')
->to($user->getEmail())
->subject('Reset your password')
->html($this->twig->render(
'resetpassword/reset_password_email.html.twig',
[
'signedUrl' => sprintf('http://localhost:3000/forgot-password/%s', $passwordToken->getToken()),
]
));
if (0 === $this->mailer->send($message)) {
throw new \RuntimeException('Unable to send email');
}
}
public function onUpdatePassword(UpdatePasswordEvent $event)
{
$passwordToken = $event->getPasswordToken();
$user = $passwordToken->getUser();
$password = $event->getPassword();
$hashedPassword = $this->passwordHasher->hashPassword($user, $password);
$user->setPassword($hashedPassword);
$this->userManager->persist($user);
$this->userManager->flush();
}