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;
}
}