Hi there! I love the way Nestjs bring us Typescript in Nodejs , it very easy and also well documented
Right now I'm building a REST API for a chat app, Http side is almost done, authentication too, I've just followed the documentation and it is working well, now I want to authenticate my user when they are trying to connect to my chat Gateway, I have tried @AuthGuard('jwt') but it seems not working, it there a way to properly do that please, thanks for your great work.
I can tell you @loicgeek how to do this, but i can't give the code.
PassportStrategy(Strategy, 'websocketStrategyName') , use ExtractJwt.fromUrlQueryParameter('jwtTokenFieldName')const jwtTokenFieldName = 'jwt token without bearer';
const socket = io('http://localhost:3000/namespace', { transports: ['websocket'], path: '/websocket-path', query: { jwtTokenFieldName } });
validate to strategy, need check user in database etc.create websocket jwt guard extending AuthGuard and pass strategy name ( websocketStrategyName )
implement method getRequest in websocket jwt guard and return handshake switchToWs().getClient().handshake.
Because we getting jwt token from url query parameter by name websocketStrategyName
But we need to get request before using strategy
By default JWT strategy getting from headers Authentication
Override get request in https://github.com/nestjs/passport/blob/master/lib/auth.guard.ts#L52
SocketIO.Socket ( context.switchToWs().getClient() ) have handshake
In handshake we can get websocket request url
fromUrlQueryParameter getting request url and parse websocketStrategyName
User automatically set into switchToWs().getClient().handshake.user analogue to HTTP request
Add @UseGuard(WsJWTGuard) to websocket gateway
Auth user in handleConnection from client first argument ( in nestjs last version Kamil created decorators for client and payload ) as client.handshake.query.jwtTokenFieldName
use jwt.verify
Or hide this implementation in websocket jwt guard.
If you have any question - notify me
@Insidexa thanks for your reply, I will try and let you know
Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I can tell you @loicgeek how to do this, but i can't give the code.
PassportStrategy(Strategy, 'websocketStrategyName'), useExtractJwt.fromUrlQueryParameter('jwtTokenFieldName')Example client
validateto strategy, need check user in database etc.create websocket jwt guard extending
AuthGuardand pass strategy name (websocketStrategyName)implement method
getRequestin websocket jwt guard and return handshakeswitchToWs().getClient().handshake.Because we getting jwt token from url query parameter by name
websocketStrategyNameBut we need to get request before using strategy
By default JWT strategy getting from headers Authentication
Override get request in https://github.com/nestjs/passport/blob/master/lib/auth.guard.ts#L52
SocketIO.Socket(context.switchToWs().getClient()) have handshakeIn handshake we can get websocket request url
fromUrlQueryParametergetting request url and parsewebsocketStrategyNameUser automatically set into
switchToWs().getClient().handshake.useranalogue to HTTP requestAdd
@UseGuard(WsJWTGuard)to websocket gatewayAuth user in
handleConnectionfromclientfirst argument ( in nestjs last version Kamil created decorators for client and payload ) asclient.handshake.query.jwtTokenFieldNameuse
jwt.verifyOr hide this implementation in websocket jwt guard.
If you have any question - notify me