Skip to content

Instantly share code, notes, and snippets.

@chunhunghan
Last active June 28, 2019 03:29
Show Gist options
  • Select an option

  • Save chunhunghan/05a7e722aa4c99c8c495117f46b5bf28 to your computer and use it in GitHub Desktop.

Select an option

Save chunhunghan/05a7e722aa4c99c8c495117f46b5bf28 to your computer and use it in GitHub Desktop.
Node.js : Websocket authentication using JWT and WS http://iostreamer.me/ws/node.js/jwt/2016/05/08/websockets_authentication.html
var ws = new WebSocketServer({
verifyClient: function (info, cb) {
var token = info.req.headers.token
if (!token)
cb(false, 401, 'Unauthorized')
else {
jwt.verify(token, 'secret-key', function (err, decoded) {
if (err) {
cb(false, 401, 'Unauthorized')
} else {
info.req.user = decoded
cb(true)
}
})
}
},
port: 8001
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment