Skip to content

Commit

Permalink
fix(telegram.ts): Fix user data resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
strd0x committed Mar 14, 2024
1 parent 45fd228 commit 9fa8e9d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 57 deletions.
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"react-device-detect": "^2.2.3",
"react-dom": "^18.2.0",
"react-router-dom": "^6.21.3",
"styled-components": "^6.1.8",
"url": "^0.11.3"
"styled-components": "^6.1.8"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.3.0",
Expand Down
61 changes: 36 additions & 25 deletions src/telegram/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { URLSearchParams } from "url";
import { Telegram } from "./web-app";
import { TelegramWebAppData } from "../models";

Expand All @@ -20,31 +19,43 @@ export const loadTelegramWebAppData = (): TelegramWebAppData => {
}

export const loadFromTelegramLib = (): TelegramWebAppData => {
if (webAppHandler === null) {
throw new Error('Telegram WebApp is not loaded.');
}
if (webAppHandler === null) {
throw new Error('Telegram WebApp is not loaded.');
}

const initData = webAppHandler.initDataUnsafe;
return { ...initData, platform: webAppHandler.platform };
const initData = webAppHandler.initDataUnsafe;
return { ...initData, platform: webAppHandler.platform };
}

export const loadFromHash = (): TelegramWebAppData => {
const documentHash = document.location.hash;

if (documentHash === '') {
throw new Error('Telegram WebApp is not loaded.');
}

const hash = decodeURIComponent(decodeURIComponent(document.location.hash.substring(1)));
const urlParams = new URLSearchParams(hash);

return {
user: urlParams.get('user') ? JSON.parse(urlParams.get('user') as string) : undefined,
chat_type: urlParams.get('chat_type') as Telegram.InitData['chat_type'],
chat_instance: urlParams.get('chat_instance') || undefined,
platform: urlParams.get('tgWebAppPlatform') || 'unknown',
auth_date: urlParams.get('auth_date') ? parseInt(urlParams.get('auth_date') as string) : 0,
hash: urlParams.get('hash') || '',
start_param: urlParams.get('start_param') || undefined,
};
}
const documentHash = document.location.hash;

if (documentHash === '') {
throw new Error('Telegram WebApp is not loaded.');
}

const hash = decodeURIComponent(
decodeURIComponent(document.location.hash.substring(1)),
);
const urlParams = new URLSearchParams(hash);
const tgWebAppData = urlParams.get('tgWebAppData');

if (tgWebAppData === null) {
throw new Error('Telegram WebApp is not loaded.');
}

const userParams = new URLSearchParams(tgWebAppData);
return {
user: userParams.get('user')
? JSON.parse(userParams.get('user') as string)
: undefined,
chat_type: urlParams.get('chat_type') as Telegram.InitData['chat_type'],
chat_instance: urlParams.get('chat_instance') || undefined,
platform: urlParams.get('tgWebAppPlatform') || 'unknown',
auth_date: urlParams.get('auth_date')
? parseInt(urlParams.get('auth_date') as string)
: 0,
hash: urlParams.get('hash') || '',
start_param: urlParams.get('start_param') || undefined,
};
}

0 comments on commit 9fa8e9d

Please sign in to comment.