src/Controller/SecurityController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\NewUserType;
  5. use App\Repository\UserRepository;
  6. use App\Services\UploadImageService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     private UserRepository $userRepository;
  16.     public function __construct(UserRepository $userRepository)
  17.     {
  18.         $this->userRepository $userRepository;
  19.     }
  20.     #[Route('/login'name'app_login'methods: ['GET''POST'])]
  21.     public function login(AuthenticationUtils $authenticationUtils): Response
  22.     {
  23.         if($this->getUser() instanceof User) {
  24.             return $this->redirectToRoute('app_login');
  25.         }
  26.         // get the login error if there is one
  27.         $error $authenticationUtils->getLastAuthenticationError();
  28.         // last username entered by the user
  29.         $lastUsername $authenticationUtils->getLastUsername();
  30.         return $this->render('security/login.html.twig', [
  31.             'last_username' => $lastUsername,
  32.             'error' => $error
  33.         ]);
  34.     }
  35.     /**
  36.      * @Route("/logout", name="app_logout")
  37.      */
  38.     public function logout()
  39.     {
  40.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  41.     }
  42.     #[Route('/register'name'register')]
  43.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherUploadImageService $imageService): Response
  44.     {
  45.         if($this->getUser() instanceof User) {
  46.             return $this->redirectToRoute('home');
  47.         }
  48.         $user = new User();
  49.         $form $this->createForm(NewUserType::class, $user);
  50.         $form->handleRequest($request);
  51.         if($form->isSubmitted() && $form->isValid()) {
  52.             $user
  53.                 ->setPassword($passwordHasher->hashPassword($user$user->getPassword()))
  54.                 ->setImage($imageService->uploadImage($user->getImageFile(), $this->getParameter('profile_images_directory')))
  55.             ;
  56.             $this->userRepository->save($usertrue);
  57.             return $this->redirectToRoute('home');
  58.         }
  59.         return $this->render('security/register.twig', [
  60.             'form' => $form->createView(),
  61.             'errors'    => $form->getErrors()
  62.         ]);
  63.     }
  64. }