allow only one connection per user, TODO: give a firendly message to users
This commit is contained in:
parent
2c41359b9d
commit
5b34130ef4
1 changed files with 10 additions and 1 deletions
|
@ -41,6 +41,14 @@ GatewayServer.prototype.eventSetup = function() {
|
|||
if (!token) return this.authDisconnect(socket, callback);
|
||||
if (!(typeof token === 'string')) return this.authDisconnect(socket, callback);
|
||||
|
||||
const allSockets = this._gateway.sockets;
|
||||
for (let [_, e] of allSockets) {
|
||||
if (e.user && e.user.token === token) {
|
||||
console.log(`[E] [gateway] [handshake] User ${e.user.username} tried to connect more than once, rejecting connection...`);
|
||||
return this.authDisconnect(socket, callback);
|
||||
}
|
||||
}
|
||||
|
||||
jwt.verify(token, secret.jwtPrivateKey, {}, async (err, data) => {
|
||||
if (err) return this.authDisconnect(socket, callback);
|
||||
if (!data) return this.authDisconnect(socket, callback);
|
||||
|
@ -59,7 +67,8 @@ GatewayServer.prototype.eventSetup = function() {
|
|||
|
||||
socket.user = {
|
||||
username: data.username,
|
||||
_id: user._id
|
||||
_id: user._id,
|
||||
token // Maybe not secure
|
||||
};
|
||||
console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`);
|
||||
return callback();
|
||||
|
|
Reference in a new issue