<?php
namespace App\Controller\Application;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Security;
class SecurityController extends AbstractController
{
/**
* @var TranslatorInterface
*/
protected $translator;
public function __construct(
protected Security $security,
TranslatorInterface $translator = null,
) {
$this->translator = $translator;
}
/**
* @Route("/login", name="login")
*/
public function login(AuthenticationUtils $authUtils)
{
/*
* Redirect the user if he is currently logged in
*/
if($this->security->isGranted('IS_AUTHENTICATED_FULLY') || $this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirectToRoute('home');
}
/*
* Get the login error if there is one
*/
$error = $authUtils->getLastAuthenticationError();
if($error) {
$this->addFlash('error', $this->translator->trans(
$error->getMessageKey(),
$error->getMessageData(),
'security'
));
}
/*
* Last username entered by the user
*/
$lastUsername = $authUtils->getLastUsername();
return $this->render('application/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}