feat: unset cookie when logout

This commit is contained in:
Maël Gangloff
2024-07-23 17:13:01 +02:00
parent 75789b750d
commit 4334ad1f37
2 changed files with 17 additions and 2 deletions

View File

@@ -4,12 +4,19 @@ namespace App\Controller;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\RouterInterface;
class HomeController extends AbstractController
{
public function __construct(private readonly RouterInterface $router)
{
}
#[Route(path: "/", name: "index")]
public function index(): Response
{
@@ -21,4 +28,14 @@ class HomeController extends AbstractController
{
return $clientRegistry->getClient('oauth')->redirect();
}
#[Route(path: "/logout", name: "logout")]
public function logout(Security $security): ?RedirectResponse
{
$response = new RedirectResponse($this->router->generate('index'));
$response->headers->clearCookie('BEARER');
if ($security->isGranted('IS_AUTHENTICATED')) $security->logout(false);
return $response;
}
}

View File

@@ -5,7 +5,6 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Controller\DomainRefreshController;
use App\Repository\DomainRepository;
use DateTimeImmutable;
@@ -13,7 +12,6 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;