Add better validation for user registration

This commit is contained in:
Tim 2022-01-04 02:25:01 +01:00
parent d4f6b359d5
commit 42f7eff85c
4 changed files with 29 additions and 18 deletions

View File

@ -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', [

View File

@ -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
{

View File

@ -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

View File

@ -1,7 +1,7 @@
{% extends 'base/base.html.twig' %}
{% block body %}
<h1>Register</h1>
<h1 class="h3 mb-3 font-weight-normal">Register</h1>
{{ form(registrationForm) }}
{% endblock %}