Implement changing of user password
This commit is contained in:
parent
9b3c970bba
commit
b8c6d41dc9
3 changed files with 51 additions and 50 deletions
|
@ -5,6 +5,7 @@ namespace App\Controller;
|
|||
use App\Entity\Wish;
|
||||
use App\Entity\User;
|
||||
use App\Form\WishFormType;
|
||||
use App\Form\ChangePasswordFormType;
|
||||
|
||||
use App\Repository\OfferingRepository;
|
||||
use App\Repository\WishRepository;
|
||||
|
@ -14,7 +15,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
use Twig\Environment;
|
||||
|
||||
class UserController extends AbstractController
|
||||
|
@ -27,7 +30,7 @@ class UserController extends AbstractController
|
|||
}
|
||||
|
||||
#[Route('/user', name: 'user_page')]
|
||||
public function user(OfferingRepository $offeringRepository): Response
|
||||
public function user(OfferingRepository $offeringRepository, Request $request, UserPasswordHasherInterface $passwordEncoder): Response
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
|
@ -36,9 +39,27 @@ class UserController extends AbstractController
|
|||
$this->addFlash('error','Your email is not verified, please check your inbox');
|
||||
}
|
||||
|
||||
$form = $this->createForm(ChangePasswordFormType::class);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user->setPassword(
|
||||
$passwordEncoder->hashPassword(
|
||||
$user,
|
||||
$form->get('plainPassword')->getData()
|
||||
)
|
||||
);
|
||||
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->persist($user);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash("success", "Successfully changed the password!");
|
||||
}
|
||||
|
||||
return $this->render('user/index.html.twig', [
|
||||
'user' => $user,
|
||||
'offers' => $offeringRepository->findByUser($user),
|
||||
'changePassword_form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Form;
|
|||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
|
@ -39,6 +40,7 @@ class ChangePasswordFormType extends AbstractType
|
|||
// this is read and encoded in the controller
|
||||
'mapped' => false,
|
||||
])
|
||||
->add('submit', SubmitType::class)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue