Correct mercure token route
This commit is contained in:
parent
d884ff4155
commit
9d37a7c549
|
|
@ -34,10 +34,9 @@ export default class extends Controller {
|
||||||
async connectToMercure() {
|
async connectToMercure() {
|
||||||
try {
|
try {
|
||||||
// Fetch the JWT token and topic from the server
|
// 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();
|
const data = await response.json();
|
||||||
|
|
||||||
console.log('Mercure token data:', data);
|
|
||||||
|
|
||||||
// Use server-provided topic if available, otherwise fallback to default per-user topic
|
// 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}`;
|
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);
|
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 {
|
try {
|
||||||
this.eventSource = new EventSource(url.toString());
|
this.eventSource = new EventSource(url.toString());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -62,11 +56,9 @@ export default class extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.eventSource.onopen = () => {
|
this.eventSource.onopen = () => {
|
||||||
console.log('✅ Mercure connection established successfully!');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.eventSource.onmessage = (event) => {
|
this.eventSource.onmessage = (event) => {
|
||||||
console.log('📨 New notification received:', event.data);
|
|
||||||
try {
|
try {
|
||||||
const notification = JSON.parse(event.data);
|
const notification = JSON.parse(event.data);
|
||||||
this.handleNewNotification(notification);
|
this.handleNewNotification(notification);
|
||||||
|
|
@ -76,7 +68,6 @@ export default class extends Controller {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.eventSource.onerror = (error) => {
|
this.eventSource.onerror = (error) => {
|
||||||
console.error('❌ Mercure connection error:', error);
|
|
||||||
try {
|
try {
|
||||||
console.error('EventSource readyState:', this.eventSource.readyState);
|
console.error('EventSource readyState:', this.eventSource.readyState);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use Lcobucci\JWT\Configuration;
|
||||||
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||||
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
|
||||||
|
class MercureController extends AbstractController
|
||||||
|
{
|
||||||
|
#[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(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -113,32 +113,5 @@ class NotificationController extends AbstractController
|
||||||
return new JsonResponse(['success' => true]);
|
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(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue