<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
class LoginController extends AbstractController
{
#[Route('/login', name: 'app_login')]
public function index(AuthenticationUtils $authenticationUtils, Request $request): Response
{
$request = $this->get('request_stack')->getCurrentRequest();
$eventId = $request->cookies->get('current_event_id');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if(!is_null($eventId)) {
return $this->render('login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
// Le cookie n'existe pas, donc nous allons le créer avec une valeur par défaut de 1
$cookie = new Cookie('current_event_id', 1, time() + (2 * 365 * 24 * 60 * 60)); // expire dans 2 ans
// Créez une nouvelle réponse pour pouvoir ajouter le cookie
$response = $this->render('login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
$response->headers->setCookie($cookie);
// Retournez la réponse qui contient maintenant le cookie
return $response;
}
}