Registration and login perfect

This commit is contained in:
Olivier PARPAILLON
2024-11-19 15:58:04 +01:00
parent dc01c2e216
commit e10becf01d
10 changed files with 118 additions and 171 deletions

View File

@@ -4,6 +4,8 @@ namespace App\Controller;
use App\Entity\Participant;
use App\Form\RegistrationFormType;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Schema\Exception\UniqueConstraintDoesNotExist;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
@@ -35,8 +37,13 @@ class RegistrationController extends AbstractController
$user->setAdministrateur(false);
}
$entityManager->persist($user);
$entityManager->flush();
try {
$entityManager->persist($user);
$entityManager->flush();
} catch (UniqueConstraintViolationException $e) {
$this->addFlash('error', 'Cet email ou pseudo est déjà utilisé, veuillez en choisir un autre.');
return $this->redirectToRoute('app_register');
}
// do anything else you need here, like send an email

View File

@@ -8,7 +8,6 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: ParticipantRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
@@ -134,7 +133,7 @@ class Participant implements UserInterface, PasswordAuthenticatedUserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
return (string) $this->pseudo;
}
/**

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Security;
use App\Entity\Participant;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class LoginFormAuthenticator extends AbstractAuthenticator
{
private $entityManager;
private $urlGenerator;
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
}
public function supports(Request $request): ?bool
{
return $request->getPathInfo() === '/login' && $request->isMethod('POST');
}
public function authenticate(Request $request): Passport
{
$identifier = $request->request->get('username');
$password = $request->request->get('password');
// Chercher l'utilisateur par email ou pseudo
$user = $this->entityManager->getRepository(Participant::class)->findOneBy([
'email' => $identifier
]) ?? $this->entityManager->getRepository(Participant::class)->findOneBy([
'pseudo' => $identifier
]);
if (!$user) {
throw new AuthenticationException('Identifiant ou mot de passe incorrect.');
}
// Utilisation de UserBadge au lieu de Participant directement
return new Passport(
new UserBadge($user->getEmail()), // Utilisation de l'email ou du pseudo pour l'identification
new PasswordCredentials($password) // Ajout du mot de passe pour la vérification
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?RedirectResponse
{
return new RedirectResponse($this->urlGenerator->generate('home'));
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?RedirectResponse
{
return new RedirectResponse($this->urlGenerator->generate('app_login'));
}
}

View File

@@ -1,45 +0,0 @@
<?php
namespace App\Security;
use App\Entity\Participant;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
//use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class UserProvider implements UserProviderInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function loadUserByIdentifier(string $username): UserInterface
{
// Cherche par email ou pseudo
$user = $this->entityManager->getRepository(Participant::class)
->findOneBy(['email' => $username]) // Cherche par email
?? $this->entityManager->getRepository(Participant::class)
->findOneBy(['pseudo' => $username]); // Ou par pseudo
if (!$user) {
throw new UsernameNotFoundException('Utilisateur non trouvé');
}
return $user;
}
public function refreshUser(UserInterface $user): UserInterface
{
// Permet de rafraîchir les données d'un utilisateur (pas toujours nécessaire)
return $user;
}
public function supportsClass(string $class): bool
{
return Participant::class === $class;
}
}