<?php
namespace Customize\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\HttpFoundation\Response;
use Customize\Service\LogoutDebugService;
class LogoutEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
LogoutEvent::class => 'onLogout',
];
}
public function onLogout(LogoutEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
// DEBUG: Log thông tin LogoutEvent
var_dump('=== DEBUG LOGOUT EVENT ===');
var_dump('Event Type: LogoutEvent');
var_dump('Request Path: ' . $request->getPathInfo());
var_dump('Is Admin Path: ' . ($request->getPathInfo()->startsWith('/admin') ? 'true' : 'false'));
var_dump('Response Status: ' . $response->getStatusCode());
var_dump('=== END DEBUG LOGOUT EVENT ===');
// Sử dụng LogoutDebugService để ghi log
$debugService = new LogoutDebugService();
$debugService->logLogoutEvent('LogoutEvent', [
'path' => $request->getPathInfo(),
'is_admin' => $request->getPathInfo()->startsWith('/admin'),
'response_status' => $response->getStatusCode()
]);
// Chỉ xử lý cho customer logout, không phải admin
if (!$request->getPathInfo()->startsWith('/admin')) {
$host = $request->getHost();
$isSecure = $request->isSecure();
// Đảm bảo cookie remember_me được xóa với thuộc tính chính xác
// Cookie có thuộc tính: Domain=hosting name, Path=/, Secure=true, HttpOnly=true, Priority=Medium
$response->headers->clearCookie('eccube_remember_me', '/', $host, $isSecure, true, false, true);
// Xóa session login_memory
$session = $request->getSession();
if ($session) {
$session->remove('_security.login_memory');
}
error_log('LogoutEventListener: Cookie remember_me đã được xóa cho path: ' . $request->getPathInfo() . ' với host: ' . $host . ', secure: ' . ($isSecure ? 'true' : 'false') . ', domain: ' . $host);
}
}
}