| const express = require("express"); |
| const axios = require("axios"); |
| const cors = require("cors"); |
|
|
| const app = express(); |
|
|
| |
| app.use(cors()); |
| app.use(express.json()); |
|
|
| app.post("/login", async (req, res) => { |
| try { |
| const { tgWebAppData } = req.body; |
| if (!tgWebAppData) { |
| return res.status(400).json({ error: "Missing tgWebAppData in body" }); |
| } |
|
|
| |
| 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); |
|
|
| |
| 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) }, |
| }); |
| } |
|
|
| |
| 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]); |
|
|
| |
| 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), |
| }, |
| }); |
| } |
|
|
| |
| 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") |
| ); |