diff --git a/src/index.js b/src/index.js index cb90205..78410cb 100644 --- a/src/index.js +++ b/src/index.js @@ -35,16 +35,22 @@ wss.on('connection', (client) => { let parsed = JSON.parse(message); let token = parsed.token; let currentGroup = parsed.currentGroup; + try { + if (!jwt.verify(token, config.secret)) { + client.send("Invalid token"); + return; + } - if (!jwt.verify(token, config.secret)) { - client.send("Invalid token"); + if (!await UserService.isInGroup(jwt.decode(token, config.secret).login.id, currentGroup)) { + client.send("Not a member of specified group"); + return; + } + } catch (e) { + log.error("Error during connection through websocket.") + client.send("Error") return; } - if (!await UserService.isInGroup(jwt.decode(token, config.secret).login.id, currentGroup)) { - client.send("Not a member of specified group"); - return; - } clients.push({ socket: client, @@ -69,7 +75,7 @@ const server = app.listen(config.port, () => { server.on('upgrade', (req, res, head) => { wss.handleUpgrade(req, res, head, socket => { - wss.emit('connection', socket, request); + wss.emit('connection', socket, req); }); });