src/Controller/SecurityController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Repository\UserRepository;
  6. use App\Repository\ExpertRepository;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // if ($this->getUser()) {
  21.         //     return $this->redirectToRoute('target_path');
  22.         // }
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error,'error2' => '','success'=>'']);
  28.     }
  29.     /**
  30.      * @Route("/register", name="register", methods={"GET", "POST"})
  31.      */
  32.     public function new(UserPasswordHasherInterface $passwordHasherRequest $requestUserRepository $userRepository): Response
  33.     {
  34.         $user = new User();
  35.         $form $this->createForm(UserType::class, $user);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $users $userRepository->findByUsername($user->getUsername());
  39.             $error="";
  40.             if ($user->getPassword() != $form['password2']->getData())
  41.                 $error="Error: Password don't match!";
  42.             if (count($users)>0)    $error="Error: This email has already been registered!";
  43.             if ($error != "")       return $this->render('user/new.html.twig', ['user' => $user,'form' => $form->createView(),'error' => $error]); 
  44.             $hashedPassword $passwordHasher->hashPassword(
  45.                 $user,
  46.                 $user->getPassword()
  47.             );
  48.             $user->setPassword($hashedPassword);
  49.             $userRepository->add($usertrue);
  50.             $this->addFlash('success''Your account has been sucessfully created!');
  51.             return $this->redirectToRoute('accueil'); 
  52.         }
  53.         return $this->renderForm('user/new.html.twig', [
  54.             'user' => $user,
  55.             'form' => $form,
  56.             'error' => '',
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/logout", name="app_logout")
  61.      */
  62.     public function logout(): void
  63.     {
  64.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  65.     }
  66. }