I’ve created a listener that checks for user inactivity, the point of it is to log them out and send them to a default page with a message that they’ve been logged out. So far I’m trying to just send them to the default page, but im unable to do even that
the listener:
class SessionTimeoutListener implements EventSubscriberInterface
{
private $requestStack;
private $csrfTokenManager;
public function __construct(CsrfTokenManagerInterface $csrfTokenManager, RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->csrfTokenManager = $csrfTokenManager;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['onRequest', 100],
['refreshSessionCookie', 0],
],
];
}
public function refreshSessionCookie(ResponseEvent $event)
{
$session = $event->getRequest()->getSession();
if ($session->isStarted() && $event->getResponse()) {
$response = $event->getResponse();
$cookieLifetime = 20;
$lastUsed = new Cookie($session->getName(), $session->getId(), time() + $cookieLifetime);
$response->headers->setCookie($lastUsed);
}
}
public function onRequest(RequestEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if (!$request->hasSession()) {
return;
}
if (!$event->isMainRequest()) {
return;
}
if (!$request->getSession()->has('_security_main')) {
return;
}
if (!$request->cookies->has('accepted_cookies')) {
return;
}
$session = $request->getSession();
if (!$session->isStarted()) {
return;
}
$lastUsedTimestamp = $session->getMetadataBag()->getLastUsed();
$currentTime = time();
$elapsedTime = $currentTime - $lastUsedTimestamp;
$sessionTimeout = 20;
if ($elapsedTime > $sessionTimeout) {
$session->invalidate();
$this->csrfTokenManager->refreshToken('security.csrf.token_manager');
$logoutUrl = $request->getUriForPath('/cs/image-list');
$response = new RedirectResponse($logoutUrl);
$event->setResponse($response);
}
}
}
framework setting
session:
handler_id: 'session.handler.native_file'
cookie_secure: true
cookie_samesite: lax
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_lifetime: 20
gc_maxlifetime: 86400
services
SymfonyComponentHttpFoundationRequestStack: '@request_stack'
AppEventListenerSessionTimeoutListener:
tags:
- { name: kernel.event_subscriber }
arguments:
- '@security.csrf.token_manager'
- '@request_stack'
# Add the url_generator service definition as an alias for the router service
session.handler.native_file:
class: SymfonyComponentHttpFoundationSessionStorageHandlerNativeFileSessionHandler
arguments: [ '%kernel.project_dir%/var/sessions/%kernel.environment%' ]
session:
class: SymfonyComponentHttpFoundationSessionSession
public: true
I am using symfony 6.2
I have tried a method with url generator and do
$logoutUrl = $this->urlGenerator->generate('homepage_default', ['_locale' => 'cs']);
$response = new RedirectResponse($logoutUrl);
return $response->send();
$event->setResponse($response);
which did not work
What I’m expecting to happen is that when a user clicks on a button that normally sends the user to a different path, they will instead be signed out and sent to homepage_default if theyve been inactive long enough, I think thats what the code is supposed to do, but I am most likely missing something or dont understand some sort of function.
Any help would be greatly appreciated