Parcourir la source

Gestion du login terminée

Sangfroid il y a 2 mois
Parent
commit
450b9f253e

+ 28 - 24
src/Controller/SecurityController.php

@@ -2,8 +2,10 @@
 
 namespace App\Controller;
 
+use App\Form\LoginType;
 use App\Repository\UserRepository;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\Form\FormError;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Notifier\NotifierInterface;
@@ -28,37 +30,39 @@ class SecurityController extends AbstractController
         Request $request
     ): Response
     {
-        // check if form is submitted
-        if ($request->isMethod('POST')) {
-            // load the user in some way (e.g. using the form input)
-            $email = $request->getPayload()->get('email');
-            $user = $userRepository->findOneBy(['email' => $email]);
+        $form = $this->createForm(LoginType::class);
+        $form->handleRequest($request);
 
+        if ($form->isSubmitted() && $form->isValid()) {
+            $email = $form->get('email')->getData();
+            $user = $userRepository->findOneBy(['email' => $email]);
             if ($user) {
-                
-                            // create a login link for $user this returns an instance
-                            // of LoginLinkDetails
-                            $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
-                            $loginLink = $loginLinkDetails->getUrl();
-                
-                            //dump($loginLink);
-                            // create a notification based on the login link details
-                            $notification = new LoginLinkNotification(
-                                $loginLinkDetails,
-                                'Bienvenue sur One Movie One Week' // email subject
-                            );
-                            // create a recipient for this user
-                            $recipient = new Recipient($user->getEmail());
 
-                            // send the notification to the user
-                            $notifier->send($notification, $recipient);
+                $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
+                $loginLink = $loginLinkDetails->getUrl();
 
-                            return $this->render('security/login_link_sent.html.twig');
+                $notification = new LoginLinkNotification(
+                    $loginLinkDetails,
+                    'Bienvenue sur One Movie One Week' // email subject
+                );
+                $recipient = new Recipient($user->getEmail());
+                $notifier->send($notification, $recipient);
 
+                return $this->redirectToRoute('app_login_sent');
             }
+            $form->get('email')->addError(new FormError('Aucun utilisateur avec cet email'));
         }
-
         // if it's not submitted, render the form to request the "login link"
-        return $this->render('security/request_login_link.html.twig');
+        return $this->render('security/request_login_link.html.twig', [
+            'form' => $form
+        ]);
+    }
+
+    #[Route('/sent', name: 'app_login_sent')]
+    public function loginSent(): Response
+    {
+        return $this->render('security/login_link_sent.html.twig', [
+            'message' => 'Lien de connexion envoyé avec succès.',
+        ]);
     }
 }

+ 30 - 0
src/Form/LoginType.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Form;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\Extension\Core\Type\EmailType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class LoginType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options): void
+    {
+        $builder
+            ->add('email', EmailType::class, [
+                'label' => false,
+                'attr' => [
+                    'placeholder' => 'Adresse mail'
+                ]
+            ])
+        ;
+    }
+
+    public function configureOptions(OptionsResolver $resolver): void
+    {
+        $resolver->setDefaults([
+            // Configure your form options here
+        ]);
+    }
+}

+ 6 - 1
templates/security/login_link_sent.html.twig

@@ -1 +1,6 @@
-<h1>Veuillez suivre le lien reçu par mail</h1>
+{% extends 'base.html.twig' %}
+
+{% block body %}
+    <h1>{{ message }}</h1>
+    <p>Vous pouvez fermer cette page</p>
+{% endblock %}

+ 7 - 4
templates/security/request_login_link.html.twig

@@ -1,8 +1,11 @@
 {% extends 'base.html.twig' %}
 
 {% block body %}
-<form action="{{ path('login') }}" method="POST">
-    <input type="email" name="email">
-    <button type="submit">Envoyer le lien par mail</button>
-</form>
+<div id="main-content">
+    {{ form_start(form) }}
+    {{ form_rest(form) }}
+        <button type="submit">Envoyer le lien par mail</button>
+    {{ form_end(form) }}
+    </form>
+</div>
 {% endblock %}