UNPKG

tg-mini-app-binder

Version:

A modular and extendable wrapper for the Telegram Mini App API to simplify interactions between your web app and the Telegram client.

33 lines (30 loc) 898 B
/** * @class UserModule * @description Manages user-related data from Telegram. */ export default class UserModule { /** * @param {object} tg - The Telegram WebApp object. */ constructor(tg) { this.tg = tg; } /** * Gets the user's data. * NOTE: This data is "unsafe" and should be validated on your backend using the `initData` string. * @returns {object | null} The user object or null if not available. * @example * // returns { id: 123, first_name: 'John', ... } */ getData() { return this.tg.initDataUnsafe?.user || null; } /** * Gets the raw `initData` string. * This string should be sent to your backend for validation to authenticate the user. * @returns {string} The initData string. */ getInitData() { return this.tg.initData; } }