From c0782955cdb46a82c435e562c39eef67ce643f1d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 30 Sep 2016 15:15:58 -0400 Subject: [PATCH] handle auth error --- mux-ws.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mux-ws.js b/mux-ws.js index 983665b..8e8c048 100644 --- a/mux-ws.js +++ b/mux-ws.js @@ -81,22 +81,30 @@ require('cluster-store').create().then(function (store) { wss.on('connection', function (ws) { var location = url.parse(ws.upgradeReq.url, true); //var token = jwt.decode(location.query.access_token); - var token = jwt.verify(location.query.access_token, secret); + var token; + try { + token = jwt.verify(location.query.access_token, secret); + } catch(e) { + token = null; + } + + /* if (!token || !token.name) { console.log('location, token'); console.log(location.query.access_token); console.log(token); } + */ if (!token) { - ws.send({ error: { message: "invalid access token", code: "E_INVALID_TOKEN" } }); + ws.send(JSON.stringify({ error: { message: "invalid access token", code: "E_INVALID_TOKEN" } })); ws.close(); return; } if (!token.name) { - ws.send({ error: { message: "invalid server name", code: "E_INVALID_NAME" } }); + ws.send(JSON.stringify({ error: { message: "invalid server name", code: "E_INVALID_NAME" } })); ws.close(); return; }