Skip to content

Commit

Permalink
feat: Added route clear-session-data (close #1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jun 1, 2023
1 parent 3ba21a2 commit b5e0a15
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/controller/miscController.ts
Expand Up @@ -15,9 +15,12 @@
*/

import { Request, Response } from 'express';
import fs from 'fs';

import { logger } from '..';
import config from '../config';
import { backupSessions, restoreSessions } from '../util/manageSession';
import { clientsArray } from '../util/sessionUtil';

export async function backupAllSessions(req: Request, res: Response) {
/**
Expand Down Expand Up @@ -133,3 +136,50 @@ export async function takeScreenshot(req: Request, res: Response) {
});
}
}

export async function clearSessionData(req: Request, res: Response) {
/**
#swagger.tags = ["Misc"]
#swagger.autoBody=false
#swagger.parameters["secretkey"] = {
required: true,
schema: 'THISISMYSECURETOKEN'
}
#swagger.parameters["session"] = {
schema: 'NERDWHATS_AMERICA'
}
*/

try {
const { secretkey, session } = req.params;

if (secretkey !== config.secretKey) {
return res.status(400).json({
response: 'error',
message: 'The token is incorrect',
});
}
if (req?.client?.page) {
delete clientsArray[req.params.session];
await req.client.logout();
}
const path = config.customUserDataDir + session;
const pathToken = __dirname + `../../../tokens/${session}.data.json`;
if (fs.existsSync(path)) {
await fs.promises.rm(path, {
recursive: true,
});
}
if (fs.existsSync(pathToken)) {
await fs.promises.rm(pathToken);
}
return res.status(200).json({ success: true });
} catch (error: any) {
logger.error(error);
return res.status(500).json({
status: false,
message: 'Error on clear session data',
error: error,
});
}
}
4 changes: 4 additions & 0 deletions src/routes/index.ts
Expand Up @@ -83,6 +83,10 @@ routes.post(
statusConnection,
SessionController.logOutSession
);
routes.post(
'/api/:session/:secretkey/clear-session-data',
MiscController.clearSessionData
);
routes.post(
'/api/:session/close-session',
verifyToken,
Expand Down

0 comments on commit b5e0a15

Please sign in to comment.