0

I have an exception whe I try to reset password in my app. I use Symfony 4.4 and and installed "symfonycasts/reset-password-bundle": "^1.13", the controller is the next:

class ResetPasswordController extends AbstractController {
    use ResetPasswordControllerTrait;
    private $resetPasswordHelper;
    private $entityManager;
    public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, EntityManagerInterface $entityManager)
    {
        $this->resetPasswordHelper = $resetPasswordHelper;
        $this->entityManager = $entityManager;
    }

    /**
     * Display & process form to request a password reset.
     *
     * @Route("", name="app_forgot_password_request")
     */
    public function request(Request $request, MailerInterface $mailer, TranslatorInterface $translator): Response
    {
        $form = $this->createForm(ResetPasswordRequestFormType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            return $this->processSendingPasswordResetEmail(
                $form->get('email')->getData(),
                $mailer,
                $translator
            );
        }
        return $this->render('reset_password/request.html.twig', [
            'requestForm' => $form->createView(),
        ]);
    }

    private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer, TranslatorInterface $translator): RedirectResponse
    {
        $user = $this->entityManager->getRepository(User::class)->findOneBy([
            'email' => $emailFormData,
        ]);
        // Do not reveal whether a user account was found or not.
        if (!$user) {
            return $this->redirectToRoute('app_check_email');
        }
        try {
            $resetToken = $this->resetPasswordHelper->generateResetToken($user);
        } catch (ResetPasswordExceptionInterface $e) {
            return new Response(json_encode(['message' => $e->getMessage()]), 500, ['Content-Type' => 'application/json']);
            // If you want to tell the user why a reset email was not sent, uncomment
            // the lines below and change the redirect to 'app_forgot_password_request'.
            // Caution: This may reveal if a user is registered or not.
            //
            // $this->addFlash('reset_password_error', sprintf(
            //     '%s - %s',
            //     $translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_HANDLE, [], 'ResetPasswordBundle'),
            //     $translator->trans($e->getReason(), [], 'ResetPasswordBundle')
            // ));

            return $this->redirectToRoute('app_check_email');
        }

        $email = (new TemplatedEmail())
            ->from(new Address('rgutierrez@greicodex.com', 'Yoke Integration Suite'))
            ->to($user->getEmail())
            ->subject('Your password reset request')
            ->htmlTemplate('reset_password/email.html.twig')
            ->context([
                'resetToken' => $resetToken,
            ])
        ;

        $mailer->send($email);

        // Store the token object in session for retrieval in check-email route.
        $this->setTokenObjectInSession($resetToken);

        return $this->redirectToRoute('app_check_email');
    }
}

I called the request function from app and the problem occured when the processSendingPasswordResetEmail function is executed in the line "$resetToken = $this->resetPasswordHelper->generateResetToken($user);" , the function is not executed and returns the following exception:

An exception occurred while executing a query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'expires_at' in 'where clause'

I followed the installation guide to add reset password feature on the next page:

https://symfony.com/doc/4.4/security/reset_password.html
https://github.com/symfonycasts/reset-password-bundle

I did not add any function, in fact everything is created by the guide as indicated and I do not know why the error occurs, could someone help me solve the problem please.

2 Answers2

0

If you execute php bin/console doc:sch:update --dump-sql, have you SQL updates ?

Regards,

  • I did this command and return this error, I think that I need to create a class, but I don't know why? and how I have to do. This is the error: Cannot autowire service "App\Controller\ResetPasswordController": argument "$resetPasswordHelper" of method "__constr uct()" references interface "SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface" but no such service exis ts. Did you create a class that implements this interface? – Rene Gutierrez Rodriguez May 26 '22 at 16:02
  • Did you register the bundle correctly ? Could you show the services.yml file ? – Yohann Daniel Carter Jun 08 '22 at 08:35
  • 1
    I already solved the problem, it turns out that when I run "composer require symfonycasts/reset-password-bundle" to add this bundle, the reset_password.yaml file was not added, but the bundle was added correctly, so I had to run "composer remove symfonycasts/reset-password-bundle" and start again by executing "composer require symfonycasts/reset-password-bundle" again and all the files were correctly added when installing the bundle, I don't know exactly why I didn't add the file to the bundle. – Rene Gutierrez Rodriguez Jun 08 '22 at 20:12
0

Could you put ResetPasswordHelperInterface $resetPasswordHelper, EntityManagerInterface $entityManager directly in your processSendingPasswordResetEmail function via Dependency Injection and remove your constructor ? You can remove the variable declaration too.

This will fix the issue.

Regards