| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace App\Controller;
- use App\Entity\User;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Session\Session;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\Form\Extension\Core\Type\PasswordType;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- class LoginController extends AbstractController
- {
- /**
- * @Route("/login", name="login")
- */
- public function index(Request $request, UserPasswordEncoderInterface $encoder): Response
- {
- $session = $this->get('session');
- if (null !== $session->get('user')) {
- $session->save();
- return $this->redirectToRoute('profile', ['id' => $session->get('user')]);
- }
- // $profile = new User();
- // Instanciation du fromBuilder
- $formBuilder = $this->createFormBuilder(); //$profile);
- // Ajout des champs
- $formBuilder
- ->add('identifiant', TextType::class)
- ->add('mot_de_passe', PasswordType::class)
- ->setAction($this->generateUrl('login'));
- // Génération du formulaire
- $form = $formBuilder->getForm();
- if ($request->getMethod() == 'POST') {
- $form->handleRequest($request);
- if ($form->isValid()) {
- $id = $form->get("identifiant")->getData();
- $passwd = $form->get('mot_de_passe')->getData();
- $em = $this->getDoctrine()->getManager();
- $repository_profile = $em->getRepository(User::class);
- $profile = $repository_profile->findOneBy(array('username' => $id));
- if ($profile) {
- if ($encoder->isPasswordValid($profile, $passwd)) {
- $session->set('user', $id);
- return $this->redirectToRoute('profile', ['username' => $session->get('user')]);
- }
- }
- return $this->render('login/index.html.twig', [
- 'message' => "Utilisateur ou mot de passe incorrect",
- 'form' => $form->createView()
- ]);
- }
- }
- return $this->render('login/index.html.twig', [
- 'form' => $form->createView(),
- 'message' => ""
- ]);
- }
- }
|