diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index f71e182..32a3797 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\User; use App\Form\RegistrationFormType; +use App\Repository\UserRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -17,7 +18,7 @@ class RegistrationController extends AbstractController public function register( Request $request, UserPasswordHasherInterface $userPasswordHasher, - EntityManagerInterface $entityManager + EntityManagerInterface $entityManager, ): Response { $user = new User(); @@ -25,19 +26,27 @@ class RegistrationController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - // encode the plain password - $user->setPassword( - $userPasswordHasher->hashPassword( - $user, - $form->get('plainPassword')->getData() - ) - ); + // Check for duplicate username/email + if ($form->get('plainPassword')->getData() !== $form->get('plainPasswordRepeated')->getData()) { + $this->addFlash('error', 'Password and password repeated must be the same'); + } else { + // encode the plain password + $user->setPassword( + $userPasswordHasher->hashPassword( + $user, + $form->get('plainPassword')->getData() + ) + ); - $entityManager->persist($user); - $entityManager->flush(); - // do anything else you need here, like send an email + $entityManager->persist($user); + $entityManager->flush(); + // do anything else you need here, like send an email + + $this->addFlash('success', sprintf('Successfully registered user %s', $user->getUsername())); + + return $this->redirectToRoute('app_test_test1'); + } - return $this->redirectToRoute('app_test_test1'); } return $this->render('security/register.html.twig', [ diff --git a/src/Entity/User.php b/src/Entity/User.php index 3faa618..944d506 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -8,9 +8,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; -/** - * @UniqueEntity(fields={"username"}, message="There is already an account with this username") - */ +#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')] #[ORM\Entity(repositoryClass: UserRepository::class)] class User implements UserInterface, PasswordAuthenticatedUserInterface { diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index c2fa6d6..291d1a8 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -24,6 +24,7 @@ class RegistrationFormType extends AbstractType // this is read and encoded in the controller 'mapped' => false, 'attr' => ['autocomplete' => 'new-password'], + 'label' => 'Password', 'constraints' => [ new NotBlank([ 'message' => 'Please enter a password', @@ -36,6 +37,10 @@ class RegistrationFormType extends AbstractType ]), ], ]) + ->add('plainPasswordRepeated', PasswordType::class, [ + 'mapped' => false, + 'label' => 'Password repeated', + ]) ->add('agreeTerms', CheckboxType::class, [ 'mapped' => false, 'constraints' => [ @@ -44,8 +49,7 @@ class RegistrationFormType extends AbstractType ]), ], ]) - ->add('register', SubmitType::class) - ; + ->add('register', SubmitType::class); } public function configureOptions(OptionsResolver $resolver): void diff --git a/templates/security/register.html.twig b/templates/security/register.html.twig index d558e9e..dc33db2 100644 --- a/templates/security/register.html.twig +++ b/templates/security/register.html.twig @@ -1,7 +1,7 @@ {% extends 'base/base.html.twig' %} {% block body %} -

Register

+

Register

{{ form(registrationForm) }} {% endblock %}