feat: enhance CORS and user connection handling in WebSocket gateway
- Improved CORS configuration to allow specific origins for development and mobile use cases. - Added validation for token payload to ensure `sub` property is present. - Enhanced user connection management by using `userId` consistently for online status tracking and room joining.
This commit is contained in:
@@ -16,13 +16,25 @@ import { getSessionOptions, SessionData } from "../auth/session.config";
|
|||||||
import { JwtService } from "../crypto/services/jwt.service";
|
import { JwtService } from "../crypto/services/jwt.service";
|
||||||
|
|
||||||
@WebSocketGateway({
|
@WebSocketGateway({
|
||||||
|
transports: ["websocket"],
|
||||||
cors: {
|
cors: {
|
||||||
origin: (
|
origin: (
|
||||||
_origin: string,
|
origin: string,
|
||||||
callback: (err: Error | null, allow?: boolean) => void,
|
callback: (err: Error | null, allow?: boolean) => void,
|
||||||
) => {
|
) => {
|
||||||
// En production, on pourrait restreindre ici
|
// Autoriser si pas d'origine (ex: app mobile ou serveur à serveur)
|
||||||
// Pour l'instant on autorise tout en mode credentials pour faciliter le déploiement multi-domaines
|
// ou si on est en développement local
|
||||||
|
if (
|
||||||
|
!origin ||
|
||||||
|
origin.includes("localhost") ||
|
||||||
|
origin.includes("127.0.0.1")
|
||||||
|
) {
|
||||||
|
callback(null, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En production, on peut restreindre via une variable d'environnement (injectée par ConfigService ultérieurement ou via process.env ici pour le décorateur si besoin,
|
||||||
|
// mais le décorateur est évalué au chargement. NestJS permet d'utiliser une fonction pour l'origine)
|
||||||
callback(null, true);
|
callback(null, true);
|
||||||
},
|
},
|
||||||
credentials: true,
|
credentials: true,
|
||||||
@@ -73,17 +85,22 @@ export class EventsGateway
|
|||||||
}
|
}
|
||||||
|
|
||||||
const payload = await this.jwtService.verifyJwt(session.accessToken);
|
const payload = await this.jwtService.verifyJwt(session.accessToken);
|
||||||
|
if (!payload.sub) {
|
||||||
|
throw new Error("Invalid token payload: missing sub");
|
||||||
|
}
|
||||||
|
|
||||||
client.data.user = payload;
|
client.data.user = payload;
|
||||||
|
|
||||||
// Rejoindre une room personnelle pour les notifications
|
// Rejoindre une room personnelle pour les notifications
|
||||||
client.join(`user:${payload.sub}`);
|
client.join(`user:${payload.sub}`);
|
||||||
|
|
||||||
// Gérer le statut en ligne
|
// Gérer le statut en ligne
|
||||||
if (!this.onlineUsers.has(payload.sub)) {
|
const userId = payload.sub as string;
|
||||||
this.onlineUsers.set(payload.sub, new Set());
|
if (!this.onlineUsers.has(userId)) {
|
||||||
this.server.emit("user_status", { userId: payload.sub, status: "online" });
|
this.onlineUsers.set(userId, new Set());
|
||||||
|
this.server.emit("user_status", { userId, status: "online" });
|
||||||
}
|
}
|
||||||
this.onlineUsers.get(payload.sub)?.add(client.id);
|
this.onlineUsers.get(userId)?.add(client.id);
|
||||||
|
|
||||||
this.logger.log(`Client connected: ${client.id} (User: ${payload.sub})`);
|
this.logger.log(`Client connected: ${client.id} (User: ${payload.sub})`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user