import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { WsException } from '@nestjs/websockets'; @Injectable() export class WsAuthGuard { constructor(private jwt: JwtService) {} validateClient(client: any, req?: any): { sub: number; email: string } | null { try { const rawUrl = req?.url || client.url || client._url; const url = new URL(rawUrl, 'http://localhost'); const token = url.searchParams.get('token'); if (!token) throw new WsException('No token'); return this.jwt.verify(token) as { sub: number; email: string }; } catch { return null; } } }