Files
duty-teller/webapp/js/auth.js
Nikolay Tatarinov 43386b15fa feat: add configurable logging level for backend and Mini App
- Introduced a new `LOG_LEVEL` configuration option in the `.env.example` file to allow users to set the logging level (DEBUG, INFO, WARNING, ERROR).
- Updated the `Settings` class to include the `log_level` attribute, normalizing its value to ensure valid logging levels are used.
- Modified the logging setup in `run.py` to utilize the configured log level, enhancing flexibility in log management.
- Enhanced the Mini App to include the logging level in the JavaScript configuration, allowing for consistent logging behavior across the application.
- Added a new `logger.js` module for frontend logging, implementing level-based filtering and console delegation.
- Included unit tests for the new logger functionality to ensure proper behavior and level handling.
2026-03-02 23:15:22 +03:00

80 lines
2.2 KiB
JavaScript

/**
* Telegram init data and access checks.
*/
import { logger } from "./logger.js";
/**
* 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) {
logger.debug("initData from hash");
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) {
logger.debug("initData from query");
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;
}
}