feat(story-1.3): user registration with auto-login and inline validation
CI / test (push) Has been cancelled

- RegistrationFormType: email + plainPassword, NotBlank/Email/Length(min=8) constraints
- SecurityController: register action hashes password, persists user, auto-logs in via Security::login()
- User entity: UniqueEntity constraint — "An account with this email already exists"
- Register Twig template: inline errors per field (role=alert), blur-validation JS
  (client fires on blur not keystroke; server-error flag prevents blur clobbering server messages)
- csrf.yaml: switched from stateless UX-dependent tokens to standard session CSRF
  (stateless token IDs require Stimulus JS to inject the real value — we removed Stimulus)

Verified: happy path → 302 + auto-login; duplicate email → 422 + inline error;
          short password → 422 + inline error

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-27 23:25:42 -04:00
parent a957c5cdd0
commit 85363e98bd
5 changed files with 248 additions and 14 deletions
+36 -4
View File
@@ -4,8 +4,14 @@ declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@@ -31,9 +37,35 @@ class SecurityController extends AbstractController
}
#[Route('/register', name: 'app_register', methods: ['GET', 'POST'])]
public function register(): Response
{
// Implemented in Story 1.3
return $this->render('security/register.html.twig');
public function register(
Request $request,
UserPasswordHasherInterface $hasher,
EntityManagerInterface $em,
Security $security,
): Response {
if ($this->getUser()) {
return $this->redirectToRoute('spa');
}
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();
$user->setPassword($hasher->hashPassword($user, $plainPassword));
$em->persist($user);
$em->flush();
$response = $security->login($user, 'form_login', 'main');
return $response ?? $this->redirectToRoute('spa');
}
return $this->render('security/register.html.twig', [
'form' => $form,
]);
}
}
+2
View File
@@ -6,11 +6,13 @@ namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[UniqueEntity(fields: ['email'], message: 'An account with this email already exists')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
+46
View File
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', EmailType::class, [
'label' => 'Email address',
'constraints' => [
new NotBlank(message: 'Please enter your email address'),
new Email(message: 'Please enter a valid email address'),
],
])
->add('plainPassword', PasswordType::class, [
'label' => 'Password',
'mapped' => false,
'constraints' => [
new NotBlank(message: 'Please enter a password'),
new Length(
min: 8,
minMessage: 'Your password must be at least {{ limit }} characters',
),
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(['data_class' => User::class]);
}
}