Skip to content

Commit

Permalink
added new SessionChecker that guards the session is not started in th…
Browse files Browse the repository at this point in the history
…e FE API
  • Loading branch information
vitek-rostislav committed Aug 17, 2022
1 parent 6fd3517 commit cf6a8ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Component\SessionChecker;

use Symfony\Component\HttpKernel\Event\ResponseEvent;

class SessionChecker
{
/**
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
*/
public function onKernelResponse(ResponseEvent $event): void
{
$request = $event->getRequest();
if (!$request->hasSession() || !$request->getSession()->isStarted() || !str_contains($request->getRequestUri(), 'graphql')) {
return;
}
$response = $event->getResponse();
$response->setContent('Session must not be started in the FE API. Check your code, please');
}
}
4 changes: 4 additions & 0 deletions packages/frontend-api/src/Resources/config/services.yaml
Expand Up @@ -39,3 +39,7 @@ services:

Lcobucci\JWT\Configuration:
factory: ['@Shopsys\FrontendApiBundle\Model\Token\JwtConfigurationFactory', create]

Shopsys\FrontendApiBundle\Component\SessionChecker\SessionChecker:
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: -999 } # must be run before the session is closed (in Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelResponse)

0 comments on commit cf6a8ea

Please sign in to comment.