diff --git a/Front-End/src/context/ChatConnectionContext.js b/Front-End/src/context/ChatConnectionContext.js index b03ff9a..ccb0c71 100644 --- a/Front-End/src/context/ChatConnectionContext.js +++ b/Front-End/src/context/ChatConnectionContext.js @@ -6,7 +6,7 @@ import React, { useRef, useCallback, } from 'react'; -import { useSession } from 'next-auth/react'; +import { useSession, getSession } from 'next-auth/react'; import logger from '@/utils/logger'; import { WS_CHAT_URL } from '@/utils/Url'; @@ -54,14 +54,15 @@ export const ChatConnectionProvider = ({ children }) => { }, []); // Configuration WebSocket - const getWebSocketUrl = (userId) => { + const getWebSocketUrl = async (userId) => { if (!userId) { logger.warn('ChatConnection: No user ID provided for WebSocket URL'); return null; } - // Récupérer le token d'authentification depuis NextAuth session - const token = session?.user?.token; + // Forcer un refresh de session pour obtenir un token JWT valide + const freshSession = await getSession(); + const token = freshSession?.user?.token; if (!token) { logger.warn( @@ -78,7 +79,7 @@ export const ChatConnectionProvider = ({ children }) => { }; // Connexion WebSocket - const connectToChat = (userId = null) => { + const connectToChat = async (userId = null) => { const userIdToUse = userId || currentUserId; // Vérifier que la session est chargée @@ -107,7 +108,7 @@ export const ChatConnectionProvider = ({ children }) => { setConnectionStatus('connecting'); try { - const wsUrl = getWebSocketUrl(userIdToUse); + const wsUrl = await getWebSocketUrl(userIdToUse); if (!wsUrl) { throw new Error(