Seamless Authorization: Fix seamless auth (#4302)

This commit is contained in:
Alexander Zinchuk 2024-03-22 13:06:24 +01:00
parent 0209cdf9dc
commit 42821109e9
6 changed files with 26 additions and 24 deletions

View File

@ -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<T extends keyof Methods>(fnName: T, ...args: Method
}
export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>) {
if (!isInited && isMasterTab) {
if (!isInited && isCurrentTabMaster()) {
const deferred = new Deferred();
apiRequestsQueue.push({ fnName, args, deferred });
return deferred.promise as MethodResponse<T>;
}
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;

View File

@ -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) {

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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;

View File

@ -7,18 +7,13 @@ import { IS_MOCKED_CLIENT } from '../config';
let parsedInitialLocationHash: Record<string, string> | 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;
}