File size: 2,299 Bytes
6b825ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// @ts-check

import fs from 'fs';
import path from 'path'; 
import { Request, Response, Application } from 'express';
import { WebSocketServer } from 'ws';
import { Server as HttpServer } from 'http';

interface LogoutRouteParams {
  req: Request;
  res: Response;
  app?: Application;
  wss?: WebSocketServer;
  wssConsole?: WebSocketServer;
  Shellwss?: WebSocketServer;
  server?: HttpServer;
}

interface LogoutExpressRouteModule {
  method: "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
  path: string;
  install: (params: Pick<LogoutRouteParams, 'req' | 'res'>) => Promise<void> | void;
}

const ACC_FILE_PATH: string = path.resolve(__dirname, '../models/data/acc.json');
const ACC_DIR_PATH: string = path.dirname(ACC_FILE_PATH);

export const modules: LogoutExpressRouteModule[] = [
  {
    method: 'post',
    path: '/logout',
    install: async ({ req, res }: Pick<LogoutRouteParams, 'req' | 'res'>): Promise<void> => {
      const html: string = `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">  
  <title>Logout</title>
  <script>
    localStorage.removeItem("exocore-token");
    localStorage.removeItem("exocore-cookies");
    // Optional: Redirect after clearing localStorage
    // window.location.href = '/login'; // Or your desired redirect path
  </script>
  <style>
    body { font-family: monospace, sans-serif; padding: 20px; }
    pre { white-space: pre-wrap; word-wrap: break-word; }
  </style>
</head>
<body>
  <p>You have been logged out. localStorage items 'exocore-token' and 'exocore-cookies' have been cleared.</p>
  <pre>{
  "status": "success"
}</pre>
</body>
</html>`;

      res.setHeader('Content-Type', 'text/html');
      res.send(html); 

      try {
        if (!fs.existsSync(ACC_DIR_PATH)) {
          await fs.promises.mkdir(ACC_DIR_PATH, { recursive: true });
        }
        await fs.promises.writeFile(ACC_FILE_PATH, JSON.stringify({}, null, 2));
      } catch (fileError: unknown) {
        const errMsg = fileError instanceof Error ? fileError.message : String(fileError);
        console.error(`Failed to clear or write acc.json during logout: ${errMsg}`, fileError);
      }
    },
  },
];