const tg = window.Telegram && window.Telegram.WebApp;
|
const status = document.getElementById("status");
|
const retry = document.getElementById("retry");
|
|
async function authenticate() {
|
if (!tg || !tg.initData) {
|
status.textContent = "Откройте панель кнопкой Mini App в Telegram.";
|
retry.classList.remove("hidden");
|
return;
|
}
|
|
tg.ready();
|
try {
|
const params = new URLSearchParams(location.search);
|
params.delete("auth");
|
const destination = "/miniapp" + (params.size ? "?" + params.toString() : "");
|
const response = await fetch("/auth/miniapp", {
|
method: "POST",
|
credentials: "same-origin",
|
headers: { "Content-Type": "application/json" },
|
body: JSON.stringify({ init_data: tg.initData })
|
});
|
const result = await response.json();
|
if (!response.ok || !result.ok) throw new Error(result.msg || "Не удалось войти.");
|
location.replace(destination);
|
} catch (error) {
|
status.textContent = error.message || "Не удалось выполнить вход. Попробуйте ещё раз.";
|
retry.classList.remove("hidden");
|
}
|
}
|
|
retry.addEventListener("click", authenticate);
|
authenticate();
|