Files
duty-teller/webapp/js/auth.js
Nikolay Tatarinov 67ba9826c7 feat: unify language handling across the application
- Updated the language configuration to use a single source of truth from `DEFAULT_LANGUAGE` for the bot, API, and Mini App, eliminating auto-detection from user settings.
- Refactored the `get_lang` function to always return `DEFAULT_LANGUAGE`, ensuring consistent language usage throughout the application.
- Modified the handling of language in various components, including API responses and UI elements, to reflect the new language management approach.
- Enhanced documentation and comments to clarify the changes in language handling.
- Added unit tests to verify the new language handling behavior and ensure coverage for the updated functionality.
2026-03-02 23:05:28 +03:00

74 lines
2.1 KiB
JavaScript

/**
* Telegram init data and access checks.
*/
/**
* Get tgWebAppData value from hash when it contains unencoded & and =.
* Value runs from tgWebAppData= until next &tgWebApp or end.
* @param {string} hash - location.hash without #
* @returns {string}
*/
export function getTgWebAppDataFromHash(hash) {
const idx = hash.indexOf("tgWebAppData=");
if (idx === -1) return "";
const start = idx + "tgWebAppData=".length;
let end = hash.indexOf("&tgWebApp", start);
if (end === -1) end = hash.length;
const raw = hash.substring(start, end);
try {
return decodeURIComponent(raw);
} catch (e) {
return raw;
}
}
/**
* Get Telegram init data string (from SDK, hash or query).
* @returns {string}
*/
export function getInitData() {
const fromSdk =
(window.Telegram && window.Telegram.WebApp && window.Telegram.WebApp.initData) || "";
if (fromSdk) return fromSdk;
const hash = window.location.hash ? window.location.hash.slice(1) : "";
if (hash) {
const fromHash = getTgWebAppDataFromHash(hash);
if (fromHash) return fromHash;
const hashParams = new URLSearchParams(hash);
const tgFromHash = hashParams.get("tgWebAppData");
if (tgFromHash) return tgFromHash;
}
const q = window.location.search
? new URLSearchParams(window.location.search).get("tgWebAppData")
: null;
if (q) {
return q;
}
return "";
}
/**
* @returns {boolean} True if host is localhost or 127.0.0.1
*/
export function isLocalhost() {
const h = window.location.hostname;
return h === "localhost" || h === "127.0.0.1" || h === "";
}
/**
* True when hash has tg WebApp params but no init data (e.g. version without data).
* @returns {boolean}
*/
export function hasTelegramHashButNoInitData() {
const hash = window.location.hash ? window.location.hash.slice(1) : "";
if (!hash) return false;
try {
const keys = Array.from(new URLSearchParams(hash).keys());
const hasVersion = keys.indexOf("tgWebAppVersion") !== -1;
const hasData = keys.indexOf("tgWebAppData") !== -1 || !!getTgWebAppDataFromHash(hash);
return hasVersion && !hasData;
} catch (e) {
return false;
}
}