|
|
@@ -2,19 +2,29 @@
|
|
|
|
|
|
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(): Response
|
|
|
+ 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();
|
|
|
|
|
|
@@ -30,8 +40,30 @@ class LoginController extends AbstractController
|
|
|
// 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->render('profile/index.html.twig');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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()
|
|
|
+ 'form' => $form->createView(),
|
|
|
+ 'message' => ""
|
|
|
]);
|
|
|
}
|
|
|
}
|