From 0e4cc6507b7194b69e91f3edd2b032b4cfdb84a6 Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 13 Nov 2024 23:08:50 +0300 Subject: [PATCH] fixes --- src/index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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); }); });