2018-06-25 01:02:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Form\LoginType;
|
2019-01-18 17:27:38 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2018-06-25 01:02:27 +00:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\{FormError, FormInterface};
|
|
|
|
use Symfony\Component\HttpFoundation\{Request, Response};
|
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
|
|
|
|
2019-01-18 17:27:38 +00:00
|
|
|
class SecurityController extends AbstractController
|
2018-06-25 01:02:27 +00:00
|
|
|
{
|
2018-06-27 23:21:07 +00:00
|
|
|
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
|
2018-06-25 01:02:27 +00:00
|
|
|
{
|
2018-06-27 23:21:07 +00:00
|
|
|
$lastError = $authenticationUtils->getLastAuthenticationError();
|
2018-06-25 01:02:27 +00:00
|
|
|
$lastUsername = $authenticationUtils->getLastUsername();
|
|
|
|
|
|
|
|
$form = $this->createLoginForm($lastUsername);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($lastError) {
|
2018-06-27 23:21:07 +00:00
|
|
|
$form->addError(new FormError($lastError->getMessage()));
|
2018-06-25 01:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('Security/login.html.twig', ['form' => $form->createView()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createLoginForm(string $username): FormInterface
|
|
|
|
{
|
|
|
|
$form = $this->createForm(LoginType::class, null, [
|
2020-01-20 17:01:51 +00:00
|
|
|
'action' => $this->generateUrl('user_auth_login'),
|
2018-06-25 01:02:27 +00:00
|
|
|
]);
|
|
|
|
$form->get('_username')->setData($username);
|
|
|
|
$form->add('submit', SubmitType::class);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|