File size: 2,902 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// @ts-check

import axios, { AxiosResponse } from 'axios';
import fs from 'fs';
import fsPromises from 'fs/promises';
import path from 'path';
import { Request, Response } from 'express';

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

const api = {
  getLink: async (): Promise<string> => {
    const res = await axios.get('https://pastebin.com/raw/YtqNc7Yi');
    if (typeof res.data === 'string' && res.data.startsWith('http')) {
      return res.data;
    }
    if (res.data && typeof res.data === 'object' && typeof res.data.link === 'string') {
      return res.data.link;
    }
    throw new Error('Failed to fetch external link from pastebin');
  },

  forgotPass: async (req: Request, res: Response) => {
    try {
      const { identifier, action, otpCode, newPass } = req.body;

      if (!identifier || !action) {
        return res.status(400).json({ error: 'Missing identifier or action' });
      }

      const link = await api.getLink();
      const apiURL = `${link}/forgot-password`;

      const body: Record<string, any> = { identifier };

      if (action === 'SendOtp') {
        body.action = 'sent';
      } else if (action === 'ResetPassword') {
        if (!otpCode || !newPass) {
          return res.status(400).json({ error: 'Missing otpCode or newPass for reset' });
        }
        body.action = 'submit';
        body.otpCode = otpCode;
        body.newPass = newPass;
      } else if (action === 'VerifyOtp') {
        if (!otpCode) {
          return res.status(400).json({ error: 'Missing otpCode for verification' });
        }
        body.action = 'verify';
        body.otpCode = otpCode;
      } else {
        return res.status(400).json({ error: 'Invalid action' });
      }

      const response: AxiosResponse = await axios.post(apiURL, body);
      const data = response.data;

      if (data.status === 'success' || data.message) {
        try {
          if (!fs.existsSync(ACC_DIR_PATH)) {
            await fsPromises.mkdir(ACC_DIR_PATH, { recursive: true });
          }
          await fsPromises.writeFile(ACC_FILE_PATH, JSON.stringify(data, null, 2));
        } catch (err) {
          console.error('[ForgotPass Route] Failed to write acc.json:', err);
        }
        return res.json({ success: true, data });
      } else {
        return res.status(400).json({ success: false, error: data.error || 'Unexpected error' });
      }
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      console.error('[ForgotPass Route] Error:', msg);
      return res.status(500).json({ error: 'Server error', details: msg });
    }
  }
};

export const modules = [
  {
    method: 'post',
    path: '/forgotpass',
    install: async ({ req, res }: { req: Request; res: Response }) => {
      return api.forgotPass(req, res);
    },
  },
];