authentication done

This commit is contained in:
Olivier PARPAILLON
2024-11-19 13:07:07 +01:00
parent 51fcbba8b2
commit 8ddbf9c5d6
7 changed files with 134 additions and 34 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Controller;
use App\Entity\Participant;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils, Security $security, Request $request): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$user = new Participant();
$formUser = $this->createForm(RegistrationFormType::class, $user);
$formUser->handleRequest($request);
return $this->render('auth/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'formUser' => $formUser,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(Security $security): ?Response
{
// throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
return $security->logout(false);
}
}

View File

@@ -6,6 +6,7 @@ use App\Entity\Participant;
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;
@@ -14,7 +15,7 @@ use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Security $security,): Response
{
$user = new Participant();
$form = $this->createForm(RegistrationFormType::class, $user);
@@ -26,16 +27,24 @@ class RegistrationController extends AbstractController
// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
$user->setRoles(['ROLE_USER']);
$user->setActif(false);
if ($user->getRoles() == 'ROLE_ADMIN') {
$user->setAdministrateur(true);
} else {
$user->setAdministrateur(false);
}
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('home');
// return $this->redirectToRoute('home');
return $security->login($user, 'form_login', 'main');
}
return $this->render('registration/register.html.twig', [
return $this->render('auth/register.html.twig', [
'registrationForm' => $form,
]);
}