我的重置密码在symfony 3中不起作用

时间:2018-11-08 16:32:22

标签: symfony reset-password

我编写了忘记密码的功能,直到现在一切正常,我必须输入新密码并单击保存。但是当我尝试使用新密码登录时,symfony会显示一个凭证不好。而且我也无法再使用旧密码登录。

控制器

   public function resettingAction(User $user, $token, Request $request)
{
  if ($user->getToken() === null || $token !== $user->getToken() || !$this->isRequestInTime($user->getPasswordRequestedAt()))
    {
        throw new AccessDeniedHttpException();
    }

    $form = $this->createForm(ResettingType::class, $user);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid())
    {
     $encoder = $this->container->get('security.password_encoder');
        $password = $encoder->encodePassword($user, $user->getSalt());
        $user->setPassword($password);
    $user->setToken(null);
        $user->setPasswordRequestedAt(null);

        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        $request->getSession()->getFlashBag()->add('success', "Votre mot de passe a été renouvelé.");

        return $this->redirectToRoute('login');

    }

    return $this->render('Resetting/index.html.twig', [
        'form' => $form->createView()
    ]);

}

ResetType

 class ResettingType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('password', RepeatedType::class, array(
            'type' => PasswordType::class,
            'first_options' => array('label' => 'Nouveau mot de passe'),
            'second_options' => array('label' => 'Confirmer le mot de passe'),
            'invalid_message' => 'Les 2 mots de passe ne sont pas identiques.',
        ))

    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        // uncomment if you want to bind to a class
        'data_class' => User::class,
    ]);
}
}

查看

 <h1>Renouvellement du mot de passe</h1>
 {{form_start(form)}}
    {{form_row(form.password.first)}}
   {{form_row(form.password.second)}}
  <button class=""  type="submit">Envoyer</button>
{{form_end(form)}}

谢谢

1 个答案:

答案 0 :(得分:1)

  

$ password = $ encoder-> encodePassword($ user,$ user-> getSalt());

在这里,您正在编码盐,而不是密码。那可能不是您想要的。我想您宁愿有这样的一行:

$password = $encoder->encodePassword($user, $user->getPassword());