diff --git a/assets/controllers/notification_controller.js b/assets/controllers/notification_controller.js index ed4d35d..14ba3f9 100644 --- a/assets/controllers/notification_controller.js +++ b/assets/controllers/notification_controller.js @@ -34,10 +34,9 @@ export default class extends Controller { async connectToMercure() { try { // Fetch the JWT token and topic from the server - const response = await fetch('/notifications/mercure-token'); + const response = await fetch('/mercure-token'); const data = await response.json(); - console.log('Mercure token data:', data); // Use server-provided topic if available, otherwise fallback to default per-user topic const topic = data.topic || `http://portail.solutions-easy.moi/notifications/user/${this.userIdValue}`; @@ -49,11 +48,6 @@ export default class extends Controller { url.searchParams.append('authorization', data.token); } - console.log('Connecting to Mercure...'); - console.log('Mercure URL:', this.mercureUrlValue); - console.log('Topic:', topic); - console.log('Full URL:', url.toString()); - try { this.eventSource = new EventSource(url.toString()); } catch (e) { @@ -62,11 +56,9 @@ export default class extends Controller { } this.eventSource.onopen = () => { - console.log('✅ Mercure connection established successfully!'); }; this.eventSource.onmessage = (event) => { - console.log('📨 New notification received:', event.data); try { const notification = JSON.parse(event.data); this.handleNewNotification(notification); @@ -76,7 +68,6 @@ export default class extends Controller { }; this.eventSource.onerror = (error) => { - console.error('❌ Mercure connection error:', error); try { console.error('EventSource readyState:', this.eventSource.readyState); } catch (e) { diff --git a/src/Controller/MercureController.php b/src/Controller/MercureController.php new file mode 100644 index 0000000..ee9237a --- /dev/null +++ b/src/Controller/MercureController.php @@ -0,0 +1,42 @@ +denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + + $topic = sprintf('http://portail.solutions-easy.moi/notifications/user/%d', $user->getId()); + + // Generate JWT token for Mercure subscription + $secret = $_ENV['MERCURE_JWT_SECRET']; + + $config = Configuration::forSymmetricSigner( + new Sha256(), + InMemory::plainText($secret) + ); + + $token = $config->builder() + ->withClaim('mercure', [ + 'subscribe' => [$topic] + ]) + ->getToken($config->signer(), $config->signingKey()); + + return new JsonResponse([ + 'token' => $token->toString(), + 'topic' => $topic, + 'userId' => $user->getId(), + ]); + } +} \ No newline at end of file diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php index f0826d6..3c7ac0c 100644 --- a/src/Controller/NotificationController.php +++ b/src/Controller/NotificationController.php @@ -113,32 +113,5 @@ class NotificationController extends AbstractController return new JsonResponse(['success' => true]); } - #[Route(path: '/mercure-token', name: 'mercure_token', methods: ['GET'])] - public function getMercureToken(): JsonResponse - { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); - $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); - $topic = sprintf('http://portail.solutions-easy.moi/notifications/user/%d', $user->getId()); - - // Generate JWT token for Mercure subscription - $secret = $_ENV['MERCURE_JWT_SECRET']; - - $config = Configuration::forSymmetricSigner( - new Sha256(), - InMemory::plainText($secret) - ); - - $token = $config->builder() - ->withClaim('mercure', [ - 'subscribe' => [$topic] - ]) - ->getToken($config->signer(), $config->signingKey()); - - return new JsonResponse([ - 'token' => $token->toString(), - 'topic' => $topic, - 'userId' => $user->getId(), - ]); - } }