mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-05 12:41:27 +00:00
fix: Chat getSession + passage en asyn ces getWebSocketUrl et connectToChat
This commit is contained in:
@ -6,7 +6,7 @@ import React, {
|
|||||||
useRef,
|
useRef,
|
||||||
useCallback,
|
useCallback,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession, getSession } from 'next-auth/react';
|
||||||
import logger from '@/utils/logger';
|
import logger from '@/utils/logger';
|
||||||
import { WS_CHAT_URL } from '@/utils/Url';
|
import { WS_CHAT_URL } from '@/utils/Url';
|
||||||
|
|
||||||
@ -54,14 +54,15 @@ export const ChatConnectionProvider = ({ children }) => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Configuration WebSocket
|
// Configuration WebSocket
|
||||||
const getWebSocketUrl = (userId) => {
|
const getWebSocketUrl = async (userId) => {
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
logger.warn('ChatConnection: No user ID provided for WebSocket URL');
|
logger.warn('ChatConnection: No user ID provided for WebSocket URL');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Récupérer le token d'authentification depuis NextAuth session
|
// Forcer un refresh de session pour obtenir un token JWT valide
|
||||||
const token = session?.user?.token;
|
const freshSession = await getSession();
|
||||||
|
const token = freshSession?.user?.token;
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
@ -78,7 +79,7 @@ export const ChatConnectionProvider = ({ children }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Connexion WebSocket
|
// Connexion WebSocket
|
||||||
const connectToChat = (userId = null) => {
|
const connectToChat = async (userId = null) => {
|
||||||
const userIdToUse = userId || currentUserId;
|
const userIdToUse = userId || currentUserId;
|
||||||
|
|
||||||
// Vérifier que la session est chargée
|
// Vérifier que la session est chargée
|
||||||
@ -107,7 +108,7 @@ export const ChatConnectionProvider = ({ children }) => {
|
|||||||
setConnectionStatus('connecting');
|
setConnectionStatus('connecting');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const wsUrl = getWebSocketUrl(userIdToUse);
|
const wsUrl = await getWebSocketUrl(userIdToUse);
|
||||||
|
|
||||||
if (!wsUrl) {
|
if (!wsUrl) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|||||||
Reference in New Issue
Block a user