Reaperxxxx commited on
Commit
b8792a5
·
verified ·
1 Parent(s): b62cb04

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +68 -31
app.js CHANGED
@@ -13,57 +13,94 @@ app.get("/login", async (req, res) => {
13
  const urlObj = new URL(url);
14
  const tgWebAppData = urlObj.hash.split("tgWebAppData=")[1]?.split("&")[0];
15
  if (!tgWebAppData) {
16
- return res.status(400).json({ error: "Invalid tgWebAppData" });
 
 
 
17
  }
18
 
19
- // Decode twice if needed
20
- let decoded = decodeURIComponent(tgWebAppData);
 
21
  try {
22
  decoded = decodeURIComponent(decoded);
 
 
 
23
  } catch (_) {}
24
 
25
  const params = new URLSearchParams(decoded);
26
 
27
- // Build initData pieces
28
- const block =
29
- `auth_date=${params.get("auth_date")}\n` +
30
- `chat_instance=${params.get("chat_instance")}\n` +
31
- `chat_type=${params.get("chat_type")}\n` +
32
- `signature=${params.get("signature")}\n` +
33
- `user=${params.get("user")}`;
34
-
35
  const hash = params.get("hash");
36
 
37
- // Here: wrap array in JSON.stringify, THEN stringify again
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  const initDataString = JSON.stringify([block, hash]);
39
 
40
- const response = await axios.post(
41
- "https://be.animekombat.com/api/auth/login",
42
- { initData: initDataString }, // pass as string, not array
43
- {
44
- headers: {
45
- Accept: "application/json, text/plain, */*",
46
- "Content-Type": "application/json",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  },
48
- }
49
- );
50
 
 
51
  res.json({
52
  token: response.data.access_token,
53
  type: response.data.token_type,
54
  expires: response.data.expires_in,
55
- user: JSON.parse(params.get("user")),
 
 
 
 
 
 
 
56
  });
57
  } catch (err) {
58
- console.error(err);
59
- if (err.response) {
60
- res.status(err.response.status).json(err.response.data);
61
- } else {
62
- res.status(500).json({ error: err.message });
63
- }
64
  }
65
  });
66
 
67
- app.listen(7860, () => {
68
- console.log("Server running at http://localhost:3000");
69
- });
 
13
  const urlObj = new URL(url);
14
  const tgWebAppData = urlObj.hash.split("tgWebAppData=")[1]?.split("&")[0];
15
  if (!tgWebAppData) {
16
+ return res.status(400).json({
17
+ error: "Invalid tgWebAppData",
18
+ debug: { hash: urlObj.hash }
19
+ });
20
  }
21
 
22
+ // Try decoding multiple times
23
+ let decoded = tgWebAppData;
24
+ let decodes = [decoded];
25
  try {
26
  decoded = decodeURIComponent(decoded);
27
+ decodes.push(decoded);
28
+ decoded = decodeURIComponent(decoded);
29
+ decodes.push(decoded);
30
  } catch (_) {}
31
 
32
  const params = new URLSearchParams(decoded);
33
 
34
+ // Extract parameters
35
+ const userRaw = params.get("user");
36
+ const auth_date = params.get("auth_date");
37
+ const chat_instance = params.get("chat_instance");
38
+ const chat_type = params.get("chat_type");
39
+ const signature = params.get("signature");
 
 
40
  const hash = params.get("hash");
41
 
42
+ // Check required values
43
+ if (!userRaw || !hash) {
44
+ return res.status(400).json({
45
+ error: "Missing required values",
46
+ debug: { decodes, params: Object.fromEntries(params) }
47
+ });
48
+ }
49
+
50
+ // Build initData block
51
+ const block =
52
+ `auth_date=${auth_date}\n` +
53
+ `chat_instance=${chat_instance}\n` +
54
+ `chat_type=${chat_type}\n` +
55
+ `signature=${signature}\n` +
56
+ `user=${userRaw}`;
57
+
58
  const initDataString = JSON.stringify([block, hash]);
59
 
60
+ // Try calling AnimeKombat
61
+ let response;
62
+ try {
63
+ response = await axios.post(
64
+ "https://be.animekombat.com/api/auth/login",
65
+ { initData: initDataString },
66
+ {
67
+ headers: {
68
+ Accept: "application/json, text/plain, */*",
69
+ "Content-Type": "application/json",
70
+ },
71
+ }
72
+ );
73
+ } catch (err) {
74
+ return res.status(err.response?.status || 500).json({
75
+ error: "AnimeKombat rejected request",
76
+ reason: err.response?.data || err.message,
77
+ debug: {
78
+ initDataString,
79
+ decodes,
80
+ params: Object.fromEntries(params),
81
  },
82
+ });
83
+ }
84
 
85
+ // Success
86
  res.json({
87
  token: response.data.access_token,
88
  type: response.data.token_type,
89
  expires: response.data.expires_in,
90
+ user: (() => {
91
+ try {
92
+ return JSON.parse(userRaw);
93
+ } catch {
94
+ return userRaw;
95
+ }
96
+ })(),
97
+ debug: { initDataString, decodes }
98
  });
99
  } catch (err) {
100
+ res.status(500).json({ error: err.message });
 
 
 
 
 
101
  }
102
  });
103
 
104
+ app.listen(7860, () =>
105
+ console.log("Server running at http://localhost:3000")
106
+ );