<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\LoginType;
use App\Form\RegistrationType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
class SecurityController extends AbstractController
{
/**
* @Route("/inscription", name="security_registration")
*/
public function registration(Request $request, EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordHasher): Response
{
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Hash user apassword before storing in database
$hashedPassword = $passwordHasher->hashPassword($user, $user->getPassword());
$user->setPassword($hashedPassword);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('security_login');
}
return $this->render('security/registration.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/connexion", name="security_login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
// Get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// Last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$form = $this->createForm(LoginType::class, [
'email' => $lastUsername,
]);
return $this->render('security/login.html.twig', [
'form' => $form->createView(),
'error' => $error,
]);
}
/**
* @Route("/deconnexion", name="security_logout")
*/
public function logout(Request $request, EventDispatcherInterface $eventDispatcher)
{
$user = $this->getUser();
$event = new LogoutEvent($request, $user);
$eventDispatcher->dispatch($event);
return $this->redirectToRoute('homepage');
}
}
Here the error : [2023-02-25T14:49:58.601108+00:00] security.DEBUG: Checking support on authenticator. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:58.601148+00:00] security.DEBUG: Authenticator does not support the request. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:59.716198+00:00] request.INFO: Matched route "security_login". {"route":"security_login","route_parameters":{"_route":"security_login","_controller":"App\\Controller\\SecurityController::login"},"request_uri":"https://localhost:8000/connexion","method":"POST"} []
[2023-02-25T14:49:59.734315+00:00] security.DEBUG: Checking for authenticator support. {"firewall_name":"main","authenticators":1} []
[2023-02-25T14:49:59.734379+00:00] security.DEBUG: Checking support on authenticator. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
[2023-02-25T14:49:59.734426+00:00] security.DEBUG: Authenticator does not support the request. {"firewall_name":"main","authenticator":"Symfony\\Component\\Security\\Http\\Authenticator\\FormLoginAuthenticator"} []
Hi, here my Symfony code :
SecurityController.php
security.yaml :
login.html.twig :