arabdullah commited on
Commit
54c8cb0
·
verified ·
1 Parent(s): 904f5c5

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +24 -13
index.js CHANGED
@@ -35,31 +35,42 @@ app.get('/checkState', (req, res) => {
35
  const { ip } = req.query;
36
 
37
  if (ip && isValidIp(ip)) {
38
- const apiKey = generateApiKey();
39
-
40
  const sessionDir = path.join(__dirname, 'session', ip);
41
  const sessionFile = path.join(sessionDir, 'info.json');
42
 
43
- if (!fs.existsSync(sessionDir)) {
44
- fs.mkdirSync(sessionDir, { recursive: true });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
 
47
- const sessionData = {
48
- ip: ip,
49
- apiKey: apiKey,
50
- };
51
-
52
- fs.writeFileSync(sessionFile, JSON.stringify(sessionData, null, 2));
53
-
54
  res.cookie('apiKey', apiKey, {
55
  maxAge: 365 * 24 * 60 * 60 * 1000,
56
  httpOnly: false,
57
- sameSite: 'None'
 
58
  });
 
59
  res.status(200).json({
60
  message: 'Server is running successfully',
61
  server: 'running',
62
- apiKey: apiKey,
63
  });
64
  } else {
65
  res.status(401).json({ message: 'Invalid IP' });
 
35
  const { ip } = req.query;
36
 
37
  if (ip && isValidIp(ip)) {
 
 
38
  const sessionDir = path.join(__dirname, 'session', ip);
39
  const sessionFile = path.join(sessionDir, 'info.json');
40
 
41
+ let apiKey;
42
+
43
+ // Check if the session file exists
44
+ if (fs.existsSync(sessionFile)) {
45
+ // Read existing API key from file
46
+ const sessionData = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
47
+ apiKey = sessionData.apiKey;
48
+ } else {
49
+ // Generate a new API key
50
+ apiKey = generateApiKey();
51
+
52
+ // Create directory for user session
53
+ if (!fs.existsSync(sessionDir)) {
54
+ fs.mkdirSync(sessionDir, { recursive: true });
55
+ }
56
+
57
+ // Save API key and IP in info.json
58
+ const sessionData = { ip, apiKey };
59
+ fs.writeFileSync(sessionFile, JSON.stringify(sessionData, null, 2));
60
  }
61
 
62
+ // Set cookie and respond with API key
 
 
 
 
 
 
63
  res.cookie('apiKey', apiKey, {
64
  maxAge: 365 * 24 * 60 * 60 * 1000,
65
  httpOnly: false,
66
+ sameSite: 'None',
67
+ secure: true // Ensure this is set for HTTPS
68
  });
69
+
70
  res.status(200).json({
71
  message: 'Server is running successfully',
72
  server: 'running',
73
+ apiKey: apiKey
74
  });
75
  } else {
76
  res.status(401).json({ message: 'Invalid IP' });