<?php
namespace App\Controller\Security;
use App\Entity\Security\User;
use App\Form\Security\RegistrationType;
use App\Security\Security\UserManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
public function __construct(
private readonly UserManager $userManager,
) {
}
#[Route(path: '/login', name: 'security_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route(path: '/logout', name: 'security_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route(path: '/register', name: 'security_register')]
public function register(Request $request): RedirectResponse|Response
{
$user = new User();
$registrationType = $this->createForm(RegistrationType::class, $user);
$registrationType->handleRequest($request);
if ($registrationType->isSubmitted() && $registrationType->isValid()) {
$this->userManager->save($user);
$this->addFlash('success', 'Inscription réalisée avec succès, vous pouvez vous connecter');
return $this->redirectToRoute('security_login');
}
return $this->render('security/register.html.twig', [
'registrationType' => $registrationType->createView(),
]);
}
}