Basically, I need to pass an authService to the "verifyClient" function inside the @WebSocketGateway
decorator, like so :
@WebSocketGateway({
transports: ['websocket'],
verifyClient: (info: { req: IncomingMessage }, done: (result: boolean, code: number, msg?: string) => void) => {
try {
const keys = authService.verify(//stuff); //how do I inject this service ?
//more stuff
done(true, 200);
} catch (error) {
done(false, 401, 'invalid token');
return;
}
}
})
export class WsGateway implements OnGatewayConnection, OnGatewayDisconnect {...
I tried doing someting like that :
function verifyClient(info: { req: IncomingMessage }, done: (result: boolean, code: number, msg?: string) => void) {
try {
const injectAuthService = Inject(AuthService);
injectAuthService(this,'authService');
const auth: AuthService = this.authService;
const keys = auth.verify(//stuff)
//more stuff
done(true, 200);
} catch (error) {
done(false, 401, 'invalid token');
return;
}
}
@WebSocketGateway({
transports: ['websocket'],
verifyClient: verifyClient
})
export class WsGateway implements OnGatewayConnection, OnGatewayDisconnect {...
based on this and this but it doesn't work, this.authService
is undefined
I faced a similar issue, but at the end, I just used the jsonwebtoken library,which is what is used under the hood by @nest/jwtService, assuming you want to verify the token as well. I couldn't find any other way to do it.