From 42821109e9707a644ea2ea708c21bc9b2daa86fd Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 22 Mar 2024 13:06:24 +0100 Subject: [PATCH] Seamless Authorization: Fix seamless auth (#4302) --- src/api/gramjs/worker/connector.ts | 15 +++++---------- src/index.tsx | 4 ++-- src/lib/gramjs/client/TelegramClient.js | 6 ++++++ src/util/establishMultitabRole.ts | 6 ++++-- src/util/handleError.ts | 6 +++--- src/util/routing.ts | 13 ++++++------- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/api/gramjs/worker/connector.ts b/src/api/gramjs/worker/connector.ts index f1d6fa8a3..ddfac7aa6 100644 --- a/src/api/gramjs/worker/connector.ts +++ b/src/api/gramjs/worker/connector.ts @@ -8,7 +8,7 @@ import type { OriginRequest, ThenArg, WorkerMessageEvent } from './types'; import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config'; import { logDebugMessage } from '../../../util/debugConsole'; import Deferred from '../../../util/Deferred'; -import { getCurrentTabId, subscribeToMasterChange } from '../../../util/establishMultitabRole'; +import { getCurrentTabId, isCurrentTabMaster } from '../../../util/establishMultitabRole'; import generateUniqueId from '../../../util/generateUniqueId'; import { pause } from '../../../util/schedulers'; import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment'; @@ -39,11 +39,6 @@ const savedLocalDb: LocalDb = { channelPtsById: {}, }; -let isMasterTab = true; -subscribeToMasterChange((isMasterTabNew) => { - isMasterTab = isMasterTabNew; -}); - const channel = IS_MULTITAB_SUPPORTED ? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel : undefined; @@ -67,7 +62,7 @@ let isInited = false; export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { updateCallback = onUpdate; - if (!isMasterTab) { + if (!isCurrentTabMaster()) { initApiOnMasterTab(initialArgs); return Promise.resolve(); } @@ -178,14 +173,14 @@ export function callApiLocal(fnName: T, ...args: Method } export function callApi(fnName: T, ...args: MethodArgs) { - if (!isInited && isMasterTab) { + if (!isInited && isCurrentTabMaster()) { const deferred = new Deferred(); apiRequestsQueue.push({ fnName, args, deferred }); return deferred.promise as MethodResponse; } - const promise = isMasterTab ? makeRequest({ + const promise = isCurrentTabMaster() ? makeRequest({ type: 'callMethod', name: fnName, args, @@ -228,7 +223,7 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) { return; } - if (isMasterTab) { + if (isCurrentTabMaster()) { cancelApiProgressMaster(messageId); } else { if (!channel) return; diff --git a/src/index.tsx b/src/index.tsx index 9dc446336..16a1ca89a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -46,7 +46,6 @@ async function init() { if (IS_MULTITAB_SUPPORTED) { subscribeToMultitabBroadcastChannel(); - await requestGlobal(APP_VERSION); localStorage.setItem(MULTITAB_LOCALSTORAGE_KEY, '1'); onBeforeUnload(() => { @@ -64,11 +63,12 @@ async function init() { getActions().updateShouldDebugExportedSenders(); if (IS_MULTITAB_SUPPORTED) { - establishMultitabRole(); subscribeToMasterChange((isMasterTab) => { getActions() .switchMultitabRole({ isMasterTab }, { forceSyncOnIOs: true }); }); + const shouldReestablishMasterToSelf = getGlobal().authState !== 'authorizationStateReady'; + establishMultitabRole(shouldReestablishMasterToSelf); } if (DEBUG) { diff --git a/src/lib/gramjs/client/TelegramClient.js b/src/lib/gramjs/client/TelegramClient.js index c639d896a..7591d8aa5 100644 --- a/src/lib/gramjs/client/TelegramClient.js +++ b/src/lib/gramjs/client/TelegramClient.js @@ -294,6 +294,9 @@ class TelegramClient { try { const ping = () => { + if (this._destroyed) { + return undefined; + } return this._sender.send(new requests.PingDelayDisconnect({ pingId: Helpers.getRandomInt(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER), disconnectDelay: PING_DISCONNECT_DELAY, @@ -331,6 +334,9 @@ class TelegramClient { if (this._sender.isReconnecting || this._isSwitchingDc) { continue; } + if (this._destroyed) { + break; + } this._sender.reconnect(); } diff --git a/src/util/establishMultitabRole.ts b/src/util/establishMultitabRole.ts index 99705d1dc..b79647edc 100644 --- a/src/util/establishMultitabRole.ts +++ b/src/util/establishMultitabRole.ts @@ -129,7 +129,7 @@ const handleMessage = ({ data }: { data: EstablishMessage }) => { } }; -export function establishMultitabRole() { +export function establishMultitabRole(shouldReestablishMasterToSelf?: boolean) { if (!channel) return; channel.addEventListener('message', handleMessage); @@ -142,6 +142,8 @@ export function establishMultitabRole() { if (masterToken === undefined) { masterToken = token; runCallbacks(true); + } else if (shouldReestablishMasterToSelf) { + reestablishMasterToSelf(); } }, ESTABLISH_TIMEOUT); @@ -183,6 +185,6 @@ export function reestablishMasterToSelf() { export const subscribeToTokenDied = addCallbackTokenDied; export const subscribeToMasterChange = addCallback; -export function isMasterTab() { +export function isCurrentTabMaster() { return masterToken === token; } diff --git a/src/util/handleError.ts b/src/util/handleError.ts index cc00c1f64..c1d8c660a 100644 --- a/src/util/handleError.ts +++ b/src/util/handleError.ts @@ -1,5 +1,5 @@ import { DEBUG, DEBUG_ALERT_MSG } from '../config'; -import { isMasterTab } from './establishMultitabRole'; +import { isCurrentTabMaster } from './establishMultitabRole'; import { throttle } from './schedulers'; let showError = true; @@ -10,7 +10,7 @@ window.addEventListener('unhandledrejection', handleErrorEvent); if (DEBUG) { window.addEventListener('focus', () => { - if (!isMasterTab()) { + if (!isCurrentTabMaster()) { return; } showError = true; @@ -21,7 +21,7 @@ if (DEBUG) { } }); window.addEventListener('blur', () => { - if (!isMasterTab()) { + if (!isCurrentTabMaster()) { return; } showError = false; diff --git a/src/util/routing.ts b/src/util/routing.ts index 1c921c4b6..5e488dfee 100644 --- a/src/util/routing.ts +++ b/src/util/routing.ts @@ -7,18 +7,13 @@ import { IS_MOCKED_CLIENT } from '../config'; let parsedInitialLocationHash: Record | undefined; let messageHash: string | undefined; let isAlreadyParsed = false; - -let LOCATION_HASH: string | undefined = window.location.hash; - -export function getInitialLocationHash() { - return LOCATION_HASH; -} +let initialLocationHash = window.location.hash; export function resetInitialLocationHash() { - LOCATION_HASH = undefined; isAlreadyParsed = false; messageHash = undefined; parsedInitialLocationHash = undefined; + initialLocationHash = ''; } export function resetLocationHash() { @@ -108,3 +103,7 @@ export function clearWebTokenAuth() { delete parsedInitialLocationHash.tgWebAuthToken; } + +function getInitialLocationHash() { + return initialLocationHash; +}