Seamless Authorization: Fix seamless auth (#4302)
This commit is contained in:
parent
0209cdf9dc
commit
42821109e9
@ -8,7 +8,7 @@ import type { OriginRequest, ThenArg, WorkerMessageEvent } from './types';
|
|||||||
import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config';
|
import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config';
|
||||||
import { logDebugMessage } from '../../../util/debugConsole';
|
import { logDebugMessage } from '../../../util/debugConsole';
|
||||||
import Deferred from '../../../util/Deferred';
|
import Deferred from '../../../util/Deferred';
|
||||||
import { getCurrentTabId, subscribeToMasterChange } from '../../../util/establishMultitabRole';
|
import { getCurrentTabId, isCurrentTabMaster } from '../../../util/establishMultitabRole';
|
||||||
import generateUniqueId from '../../../util/generateUniqueId';
|
import generateUniqueId from '../../../util/generateUniqueId';
|
||||||
import { pause } from '../../../util/schedulers';
|
import { pause } from '../../../util/schedulers';
|
||||||
import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment';
|
import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment';
|
||||||
@ -39,11 +39,6 @@ const savedLocalDb: LocalDb = {
|
|||||||
channelPtsById: {},
|
channelPtsById: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
let isMasterTab = true;
|
|
||||||
subscribeToMasterChange((isMasterTabNew) => {
|
|
||||||
isMasterTab = isMasterTabNew;
|
|
||||||
});
|
|
||||||
|
|
||||||
const channel = IS_MULTITAB_SUPPORTED
|
const channel = IS_MULTITAB_SUPPORTED
|
||||||
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
|
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
|
||||||
: undefined;
|
: undefined;
|
||||||
@ -67,7 +62,7 @@ let isInited = false;
|
|||||||
export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
|
export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
|
||||||
updateCallback = onUpdate;
|
updateCallback = onUpdate;
|
||||||
|
|
||||||
if (!isMasterTab) {
|
if (!isCurrentTabMaster()) {
|
||||||
initApiOnMasterTab(initialArgs);
|
initApiOnMasterTab(initialArgs);
|
||||||
return Promise.resolve();
|
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>) {
|
export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>) {
|
||||||
if (!isInited && isMasterTab) {
|
if (!isInited && isCurrentTabMaster()) {
|
||||||
const deferred = new Deferred();
|
const deferred = new Deferred();
|
||||||
apiRequestsQueue.push({ fnName, args, deferred });
|
apiRequestsQueue.push({ fnName, args, deferred });
|
||||||
|
|
||||||
return deferred.promise as MethodResponse<T>;
|
return deferred.promise as MethodResponse<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promise = isMasterTab ? makeRequest({
|
const promise = isCurrentTabMaster() ? makeRequest({
|
||||||
type: 'callMethod',
|
type: 'callMethod',
|
||||||
name: fnName,
|
name: fnName,
|
||||||
args,
|
args,
|
||||||
@ -228,7 +223,7 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMasterTab) {
|
if (isCurrentTabMaster()) {
|
||||||
cancelApiProgressMaster(messageId);
|
cancelApiProgressMaster(messageId);
|
||||||
} else {
|
} else {
|
||||||
if (!channel) return;
|
if (!channel) return;
|
||||||
|
|||||||
@ -46,7 +46,6 @@ async function init() {
|
|||||||
|
|
||||||
if (IS_MULTITAB_SUPPORTED) {
|
if (IS_MULTITAB_SUPPORTED) {
|
||||||
subscribeToMultitabBroadcastChannel();
|
subscribeToMultitabBroadcastChannel();
|
||||||
|
|
||||||
await requestGlobal(APP_VERSION);
|
await requestGlobal(APP_VERSION);
|
||||||
localStorage.setItem(MULTITAB_LOCALSTORAGE_KEY, '1');
|
localStorage.setItem(MULTITAB_LOCALSTORAGE_KEY, '1');
|
||||||
onBeforeUnload(() => {
|
onBeforeUnload(() => {
|
||||||
@ -64,11 +63,12 @@ async function init() {
|
|||||||
getActions().updateShouldDebugExportedSenders();
|
getActions().updateShouldDebugExportedSenders();
|
||||||
|
|
||||||
if (IS_MULTITAB_SUPPORTED) {
|
if (IS_MULTITAB_SUPPORTED) {
|
||||||
establishMultitabRole();
|
|
||||||
subscribeToMasterChange((isMasterTab) => {
|
subscribeToMasterChange((isMasterTab) => {
|
||||||
getActions()
|
getActions()
|
||||||
.switchMultitabRole({ isMasterTab }, { forceSyncOnIOs: true });
|
.switchMultitabRole({ isMasterTab }, { forceSyncOnIOs: true });
|
||||||
});
|
});
|
||||||
|
const shouldReestablishMasterToSelf = getGlobal().authState !== 'authorizationStateReady';
|
||||||
|
establishMultitabRole(shouldReestablishMasterToSelf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
|
|||||||
@ -294,6 +294,9 @@ class TelegramClient {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const ping = () => {
|
const ping = () => {
|
||||||
|
if (this._destroyed) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
return this._sender.send(new requests.PingDelayDisconnect({
|
return this._sender.send(new requests.PingDelayDisconnect({
|
||||||
pingId: Helpers.getRandomInt(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER),
|
pingId: Helpers.getRandomInt(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER),
|
||||||
disconnectDelay: PING_DISCONNECT_DELAY,
|
disconnectDelay: PING_DISCONNECT_DELAY,
|
||||||
@ -331,6 +334,9 @@ class TelegramClient {
|
|||||||
if (this._sender.isReconnecting || this._isSwitchingDc) {
|
if (this._sender.isReconnecting || this._isSwitchingDc) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (this._destroyed) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
this._sender.reconnect();
|
this._sender.reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ const handleMessage = ({ data }: { data: EstablishMessage }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function establishMultitabRole() {
|
export function establishMultitabRole(shouldReestablishMasterToSelf?: boolean) {
|
||||||
if (!channel) return;
|
if (!channel) return;
|
||||||
|
|
||||||
channel.addEventListener('message', handleMessage);
|
channel.addEventListener('message', handleMessage);
|
||||||
@ -142,6 +142,8 @@ export function establishMultitabRole() {
|
|||||||
if (masterToken === undefined) {
|
if (masterToken === undefined) {
|
||||||
masterToken = token;
|
masterToken = token;
|
||||||
runCallbacks(true);
|
runCallbacks(true);
|
||||||
|
} else if (shouldReestablishMasterToSelf) {
|
||||||
|
reestablishMasterToSelf();
|
||||||
}
|
}
|
||||||
}, ESTABLISH_TIMEOUT);
|
}, ESTABLISH_TIMEOUT);
|
||||||
|
|
||||||
@ -183,6 +185,6 @@ export function reestablishMasterToSelf() {
|
|||||||
export const subscribeToTokenDied = addCallbackTokenDied;
|
export const subscribeToTokenDied = addCallbackTokenDied;
|
||||||
export const subscribeToMasterChange = addCallback;
|
export const subscribeToMasterChange = addCallback;
|
||||||
|
|
||||||
export function isMasterTab() {
|
export function isCurrentTabMaster() {
|
||||||
return masterToken === token;
|
return masterToken === token;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { DEBUG, DEBUG_ALERT_MSG } from '../config';
|
import { DEBUG, DEBUG_ALERT_MSG } from '../config';
|
||||||
import { isMasterTab } from './establishMultitabRole';
|
import { isCurrentTabMaster } from './establishMultitabRole';
|
||||||
import { throttle } from './schedulers';
|
import { throttle } from './schedulers';
|
||||||
|
|
||||||
let showError = true;
|
let showError = true;
|
||||||
@ -10,7 +10,7 @@ window.addEventListener('unhandledrejection', handleErrorEvent);
|
|||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
window.addEventListener('focus', () => {
|
window.addEventListener('focus', () => {
|
||||||
if (!isMasterTab()) {
|
if (!isCurrentTabMaster()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
showError = true;
|
showError = true;
|
||||||
@ -21,7 +21,7 @@ if (DEBUG) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
window.addEventListener('blur', () => {
|
window.addEventListener('blur', () => {
|
||||||
if (!isMasterTab()) {
|
if (!isCurrentTabMaster()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
showError = false;
|
showError = false;
|
||||||
|
|||||||
@ -7,18 +7,13 @@ import { IS_MOCKED_CLIENT } from '../config';
|
|||||||
let parsedInitialLocationHash: Record<string, string> | undefined;
|
let parsedInitialLocationHash: Record<string, string> | undefined;
|
||||||
let messageHash: string | undefined;
|
let messageHash: string | undefined;
|
||||||
let isAlreadyParsed = false;
|
let isAlreadyParsed = false;
|
||||||
|
let initialLocationHash = window.location.hash;
|
||||||
let LOCATION_HASH: string | undefined = window.location.hash;
|
|
||||||
|
|
||||||
export function getInitialLocationHash() {
|
|
||||||
return LOCATION_HASH;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resetInitialLocationHash() {
|
export function resetInitialLocationHash() {
|
||||||
LOCATION_HASH = undefined;
|
|
||||||
isAlreadyParsed = false;
|
isAlreadyParsed = false;
|
||||||
messageHash = undefined;
|
messageHash = undefined;
|
||||||
parsedInitialLocationHash = undefined;
|
parsedInitialLocationHash = undefined;
|
||||||
|
initialLocationHash = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resetLocationHash() {
|
export function resetLocationHash() {
|
||||||
@ -108,3 +103,7 @@ export function clearWebTokenAuth() {
|
|||||||
|
|
||||||
delete parsedInitialLocationHash.tgWebAuthToken;
|
delete parsedInitialLocationHash.tgWebAuthToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getInitialLocationHash() {
|
||||||
|
return initialLocationHash;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user