From 771959ec2b3ada35f6a037e3e0ae1344b431974c Mon Sep 17 00:00:00 2001 From: mrfry Date: Wed, 16 Dec 2020 18:04:34 +0100 Subject: [PATCH] Deleting old sessions fix --- src/modules/api/api.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index 40889f6..e6f635f 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -186,7 +186,6 @@ function GetApp(): ModuleType { temp = temp[temp.length - 1] version = temp - console.log(version) } function LoadMOTD() { @@ -680,7 +679,6 @@ function GetApp(): ModuleType { .replace(/"/g, '') .replace(/;/g, '') : false - const cid = req.body.cid const isScript = req.body.script const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const user: User = dbtools.Select(userDB, 'users', { @@ -690,24 +688,32 @@ function GetApp(): ModuleType { if (user) { const sessionID = uuidv4() - const existingSessions = dbtools.Select(userDB, 'sessions', { - userID: user.id, - isScript: isScript ? 1 : 0, - }) + const existingSessions = dbtools + .Select(userDB, 'sessions', { + userID: user.id, + isScript: isScript ? 1 : 0, + }) + .sort((a, b) => { + return new Date(a).getTime() - new Date(b).getTime() + }) - if (existingSessions.length >= minimumAlowwedSessions) { + // TODO: check if sort works, and only the first few gets deleted. + + const diff = existingSessions.length - minimumAlowwedSessions + if (diff > 0) { logger.Log( `Multiple ${isScript ? 'script' : 'website'} sessions ( ${ existingSessions.length } ) for #${user.id}, deleting olds`, logger.GetColor('cyan') ) - existingSessions.forEach((sess) => { + for (let i = 0; i < diff; i++) { + const id = existingSessions[i].id dbtools.Delete(userDB, 'sessions', { - id: sess.id, + id: id, isScript: isScript ? 1 : 0, }) - }) + } } dbtools.Update( @@ -763,7 +769,7 @@ function GetApp(): ModuleType { logger.Log( `Login attempt with invalid pw: ${pw} to ${ isScript ? 'script' : 'website' - }${cid ? ', CID:' + cid : ''}`, + }`, logger.GetColor('cyan') ) res.json({