Last active
June 28, 2019 03:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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