src/Controller/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\UserRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class SecurityController extends AbstractController
  9. {
  10.     private UserRepository $userRepository;
  11.     public function __construct(
  12.         UserRepository $userRepository
  13.     ) {
  14.         $this->userRepository $userRepository;
  15.     }
  16.     /**
  17.      * @Route("/login", name="app_login")
  18.      */
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         if ($this->getUser()) {
  22.             return $this->redirectToRoute('home');
  23.         }
  24.         if (!$this->userRepository->hasUser()) {
  25.             return $this->redirectToRoute('app_register');
  26.         }
  27.         // get the login error if there is one
  28.         $error $authenticationUtils->getLastAuthenticationError();
  29.         // last username entered by the user
  30.         $lastUsername $authenticationUtils->getLastUsername();
  31.         return $this->render('security/login.html.twig', ['controller_title' => 'Login''controller_subtitle' => 'Access the dashboard''last_username' => $lastUsername'error' => $error]);
  32.     }
  33.     /**
  34.      * @Route("/logout", name="app_logout")
  35.      */
  36.     public function logout()
  37.     {
  38.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  39.     }
  40. }