<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use App\Repository\UserRepository;
use Psr\Log\LoggerInterface;
class SecurityController extends AbstractController
{
public function __construct(LoggerInterface $logger, UserRepository $userRepository)
{
$this->userRepository = $userRepository;
$this->logger = $logger;
}
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$success = null;
$customError = null;
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('admin/security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'success' => $success, 'custom_error' => $customError]);
}
#[Route(path: '/login/{token}', name: 'app_verification')]
public function Verify(string $token,AuthenticationUtils $authenticationUtils,EntityManagerInterface $entityManager): Response
{
$success = null;
$customError = null;
$user = $this->userRepository->findOneByToken($token);
if ($user) {
if ($user->isVerified()) {
$customError = "Already verified, please log in.";
} else {
$user->setIsVerified(true);
$entityManager->persist($user);
$entityManager->flush();
$success = "Verification complete, please log in.";
}
} else {
$customError = "Invalid token";
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('admin/security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'success' => $success, 'custom_error' => $customError]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}