File size: 2,674 Bytes
ded049f 138aaea e98b9b2 a9eeff2 138aaea e98b9b2 138aaea ded049f 138aaea e98b9b2 b8792a5 b62cb04 b8792a5 b62cb04 e98b9b2 b62cb04 281af0c b8792a5 b62cb04 b8792a5 138aaea b8792a5 b62cb04 e98b9b2 b8792a5 ded049f b8792a5 ded049f b8792a5 281af0c b62cb04 b8792a5 138aaea 281af0c ded049f b8792a5 ded049f e98b9b2 b8792a5 | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 | const express = require("express");
const axios = require("axios");
const cors = require("cors");
const app = express();
// Middleware
app.use(cors()); // allow all origins
app.use(express.json()); // parse JSON body
app.post("/login", async (req, res) => {
try {
const { tgWebAppData } = req.body;
if (!tgWebAppData) {
return res.status(400).json({ error: "Missing tgWebAppData in body" });
}
// Try decoding multiple times
let decoded = tgWebAppData;
let decodes = [decoded];
try {
decoded = decodeURIComponent(decoded);
decodes.push(decoded);
decoded = decodeURIComponent(decoded);
decodes.push(decoded);
} catch (_) {}
const params = new URLSearchParams(decoded);
// Extract parameters
const userRaw = params.get("user");
const auth_date = params.get("auth_date");
const chat_instance = params.get("chat_instance");
const chat_type = params.get("chat_type");
const signature = params.get("signature");
const hash = params.get("hash");
if (!userRaw || !hash) {
return res.status(400).json({
error: "Missing required values",
debug: { decodes, params: Object.fromEntries(params) },
});
}
// Build initData block
const block =
`auth_date=${auth_date}\n` +
`chat_instance=${chat_instance}\n` +
`chat_type=${chat_type}\n` +
`signature=${signature}\n` +
`user=${userRaw}`;
const initDataString = JSON.stringify([block, hash]);
// Try calling AnimeKombat
let response;
try {
response = await axios.post(
"https://be.animekombat.com/api/auth/login",
{ initData: initDataString },
{
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json",
},
}
);
} catch (err) {
return res.status(err.response?.status || 500).json({
error: "AnimeKombat rejected request",
reason: err.response?.data || err.message,
debug: {
initDataString,
decodes,
params: Object.fromEntries(params),
},
});
}
// Success
res.json({
token: response.data.access_token,
type: response.data.token_type,
expires: response.data.expires_in,
user: (() => {
try {
return JSON.parse(userRaw);
} catch {
return userRaw;
}
})(),
debug: { initDataString, decodes },
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(7860, () =>
console.log("Server running at http://localhost:3000")
); |