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

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +25 -17
app.js CHANGED
@@ -9,29 +9,37 @@ app.get("/login", async (req, res) => {
9
  return res.status(400).json({ error: "Missing ?url=" });
10
  }
11
 
12
- // Extract tgWebAppData from fragment
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 (because it's double-encoded in your example URL)
20
- const decodedOnce = decodeURIComponent(tgWebAppData);
21
- const decodedTwice = decodeURIComponent(decodedOnce);
 
 
22
 
23
- const params = new URLSearchParams(decodedTwice);
24
 
25
- // Build initData
26
- const initDataArr = [
27
- `auth_date=${params.get("auth_date")}\nchat_instance=${params.get("chat_instance")}\nchat_type=${params.get("chat_type")}\nsignature=${params.get("signature")}\nuser=${params.get("user")}`,
28
- params.get("hash"),
29
- ];
 
 
 
 
 
 
 
30
 
31
- // Send to AnimeKombat API (stringified array!)
32
  const response = await axios.post(
33
  "https://be.animekombat.com/api/auth/login",
34
- { initData: JSON.stringify(initDataArr) },
35
  {
36
  headers: {
37
  Accept: "application/json, text/plain, */*",
@@ -41,10 +49,10 @@ app.get("/login", async (req, res) => {
41
  );
42
 
43
  res.json({
44
- access_token: response.data.access_token,
45
- token_type: response.data.token_type,
46
- expires_in: response.data.expires_in,
47
- user: JSON.parse(params.get("user")), // include parsed user for convenience
48
  });
49
  } catch (err) {
50
  console.error(err);
@@ -57,5 +65,5 @@ app.get("/login", async (req, res) => {
57
  });
58
 
59
  app.listen(7860, () => {
60
- console.log("Server running on http://localhost:3000");
61
  });
 
9
  return res.status(400).json({ error: "Missing ?url=" });
10
  }
11
 
12
+ // Extract tgWebAppData
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, */*",
 
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);
 
65
  });
66
 
67
  app.listen(7860, () => {
68
+ console.log("Server running at http://localhost:3000");
69
  });