Replies: 2 comments
-
And an even older suggestion, with some potential: websockets#377 (comment) const userRequestMap = new WeakMap();
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port,
verifyClient: (info, done) => {
const user = authenticateUser(info);
userRequestMap.set(info.req, user);
done(user !== null);
},
});
wss.on('connection', (connection, request) =>{
const user = userRequestMap.get(request);
}); EDIT: Change |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another one, pretty much the same as the const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({
port: 8080, verifyClient: async ({ req }, cb) => {
const { headers } = req;
if (headers.token) {
const decodedToken = await decodeToken(headers.token);
// req.headers.decodedToken = JSON.stringify(decodedToken);
// As noted. The token can just as well be added directly to req
req.decodedToken = JSON.stringify(decodedToken);
cb(true);
} else {
cb(false, 401, 'Unauthorized');
}
}
});
wss.on('connection', (ws, req)=> {
console.log("on Connection decodedToken ==> ", JSON.parse(req.decodedToken));
}); Need expansion in the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's probably a good idea to stick to something like this:
as discussed in websockets#1966 (comment)
Same discussion, just older: websockets#377 (comment)
Beta Was this translation helpful? Give feedback.
All reactions