src/Controller/SecurityController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use App\Repository\UserRepository;
  10. use Psr\Log\LoggerInterface;
  11. class SecurityController extends AbstractController
  12. {
  13.     public function __construct(LoggerInterface $loggerUserRepository $userRepository)
  14.     {
  15.             $this->userRepository $userRepository;
  16.             $this->logger $logger;
  17.     }
  18.     #[Route(path'/login'name'app_login')]
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.                 $success null;
  23.                 $customError null;
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('admin/security/login.html.twig', ['last_username' => $lastUsername'error' => $error'success' => $success'custom_error' => $customError]);
  26.     }
  27.     #[Route(path'/login/{token}'name'app_verification')]
  28.     public function Verify(string $token,AuthenticationUtils $authenticationUtils,EntityManagerInterface $entityManager): Response
  29.     {
  30.                 $success null;    
  31.                 $customError null;
  32.                 $user $this->userRepository->findOneByToken($token);
  33.                 if ($user) {
  34.                     if ($user->isVerified()) {
  35.                         $customError "Already verified, please log in.";
  36.                     } else {
  37.                         $user->setIsVerified(true);
  38.                         $entityManager->persist($user);
  39.               $entityManager->flush();
  40.                         $success "Verification complete, please log in.";
  41.                     }
  42.                 } else {
  43.                     $customError "Invalid token";
  44.                 }
  45.         $error $authenticationUtils->getLastAuthenticationError();
  46.         $lastUsername $authenticationUtils->getLastUsername();
  47.         return $this->render('admin/security/login.html.twig', ['last_username' => $lastUsername'error' => $error'success' => $success'custom_error' => $customError]);
  48.     }
  49.     #[Route(path'/logout'name'app_logout')]
  50.     public function logout(): void
  51.     {
  52.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  53.     }
  54. }