20 lines
563 B
TypeScript
20 lines
563 B
TypeScript
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): { sub: number; email: string } | null {
|
|
try {
|
|
const url = new URL(client.url || client._url, 'http://localhost');
|
|
const token = url.searchParams.get('token');
|
|
if (!token) throw new WsException('No token');
|
|
return this.jwt.verify(token);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
}
|