<?php
// src/Controller/SecurityController.php
namespace App\Controller\Security;
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\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Repository\ClientRepository;
class SecurityController extends AbstractController
{
#[Route('/login', name: 'login')]
public function login(AuthenticationUtils $authenticationUtils): \Symfony\Component\HttpFoundation\Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
$user = $this->getUser();
if(!empty($user)){
return $this->redirectToRoute('client_espace');
die;
}
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'path_name' => 'login'
]);
}
#[Route('/force-login/{id}', name: 'force_login')]
public function forceLogin(int $id, TokenStorageInterface $tokenStorage, ClientRepository $clientRepository)
{
$client = $clientRepository->find($id);
if (!$client) {
return $this->viewHandler->handle(View::create(['message' => 'Client not found'], Response::HTTP_NOT_FOUND));
}
$token = new UsernamePasswordToken($client, 'website', $client->getRoles());
$tokenStorage->setToken($token);
$user = $this->getUser();
return $this->redirectToRoute('client_espace');
}
#[Route('/logout', name: 'logout')]
public function logout(): void
{
// Symfony gère automatiquement la déconnexion
}
}