This commit is contained in:
leca 2024-11-13 23:08:50 +03:00
parent 1e6975fa13
commit 0e4cc6507b
1 changed files with 13 additions and 7 deletions

View File

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