From c6ed04fea6faec17b7a2c1cc8e36567ef2ebc107 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 23 Jun 2021 16:29:17 +0300 Subject: [PATCH] Fix showing Main layout for non-logged users --- src/App.tsx | 9 ++-- src/api/gramjs/methods/client.ts | 7 +++- src/global/cache.ts | 8 ++-- src/modules/actions/api/initial.ts | 2 +- src/modules/actions/apiUpdaters/initial.ts | 11 ++++- src/{modules/actions/api => util}/sessions.ts | 42 ++++++++++++------- 6 files changed, 48 insertions(+), 31 deletions(-) rename src/{modules/actions/api => util}/sessions.ts (85%) diff --git a/src/App.tsx b/src/App.tsx index ebf514db3..8cff1ea31 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,9 +3,7 @@ import React, { withGlobal } from './lib/teact/teactn'; import { GlobalActions, GlobalState } from './global/types'; -import { - LEGACY_SESSION_KEY, INACTIVE_MARKER, SESSION_USER_KEY, PAGE_TITLE, -} from './config'; +import { INACTIVE_MARKER, PAGE_TITLE } from './config'; import { pick } from './util/iteratees'; import { updateSizes } from './util/windowSize'; import { addActiveTabChangeListener } from './util/activeTabMonitor'; @@ -15,6 +13,7 @@ import Auth from './components/auth/Auth'; import UiLoader from './components/common/UiLoader'; import Main from './components/main/Main.async'; import AppInactive from './components/main/AppInactive'; +import { hasStoredSession } from './util/sessions'; // import Test from './components/test/TestNoRedundancy'; type StateProps = Pick; @@ -55,9 +54,7 @@ const App: FC = ({ authState, disconnect }) => { } } - const hasSession = localStorage.getItem(SESSION_USER_KEY) || localStorage.getItem(LEGACY_SESSION_KEY); - - return hasSession ? renderMain() : ; + return hasStoredSession(true) ? renderMain() : ; }; function renderMain() { diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index cb5320ef6..50dd64bab 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -37,14 +37,15 @@ let client: TelegramClient; let isConnected = false; export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { - onUpdate = _onUpdate; - if (DEBUG) { // eslint-disable-next-line no-console console.log('>>> START INIT API'); } + onUpdate = _onUpdate; + const { sessionData, userAgent } = initialArgs; + const session = new sessions.CallbackSession(sessionData, onSessionUpdate); client = new TelegramClient( new sessions.CallbackSession(sessionData, onSessionUpdate), @@ -58,6 +59,8 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) } as any, ); + onSessionUpdate(session.getSessionData()); + client.addEventHandler(handleGramJsUpdate, gramJsUpdateEventBuilder); client.addEventHandler(updater, gramJsUpdateEventBuilder); diff --git a/src/global/cache.ts b/src/global/cache.ts index 292c83c28..7cbe17c70 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -11,13 +11,14 @@ import { GLOBAL_STATE_CACHE_DISABLED, GLOBAL_STATE_CACHE_KEY, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT, - LEGACY_SESSION_KEY, - MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN, GLOBAL_STATE_CACHE_USER_LIST_LIMIT, SESSION_USER_KEY, + MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN, + GLOBAL_STATE_CACHE_USER_LIST_LIMIT, } from '../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../util/environment'; import { pick } from '../util/iteratees'; import { INITIAL_STATE } from './initial'; import { selectCurrentMessageList } from '../modules/selectors'; +import { hasStoredSession } from '../util/sessions'; const UPDATE_THROTTLE = 1000; @@ -44,8 +45,7 @@ export function initCache() { export function loadCache(initialState: GlobalState) { if (!GLOBAL_STATE_CACHE_DISABLED) { - const hasSession = localStorage.getItem(SESSION_USER_KEY) || localStorage.getItem(LEGACY_SESSION_KEY); - if (hasSession) { + if (hasStoredSession(true)) { isAllowed = true; addCallback(updateCacheThrottled); return readCache(initialState); diff --git a/src/modules/actions/api/initial.ts b/src/modules/actions/api/initial.ts index 2876d81bf..dbf0f80bc 100644 --- a/src/modules/actions/api/initial.ts +++ b/src/modules/actions/api/initial.ts @@ -23,7 +23,7 @@ import { importLegacySession, clearLegacySessions, importTestSession, -} from './sessions'; +} from '../../../util/sessions'; addReducer('initApi', (global: GlobalState, actions) => { (async () => { diff --git a/src/modules/actions/apiUpdaters/initial.ts b/src/modules/actions/apiUpdaters/initial.ts index b7279a880..3d85f6c40 100644 --- a/src/modules/actions/apiUpdaters/initial.ts +++ b/src/modules/actions/apiUpdaters/initial.ts @@ -150,7 +150,8 @@ function onUpdateConnectionState(update: ApiUpdateConnectionState) { } function onUpdateSession(update: ApiUpdateSession) { - if (!getGlobal().authRememberMe) { + const { authRememberMe, authState } = getGlobal(); + if (!authRememberMe || authState !== 'authorizationStateReady') { return; } @@ -160,8 +161,14 @@ function onUpdateSession(update: ApiUpdateSession) { } function onUpdateServerTimeOffset(update: ApiUpdateServerTimeOffset) { + const global = getGlobal(); + + if (global.serverTimeOffset === update.serverTimeOffset) { + return; + } + setGlobal({ - ...getGlobal(), + ...global, serverTimeOffset: update.serverTimeOffset, }); } diff --git a/src/modules/actions/api/sessions.ts b/src/util/sessions.ts similarity index 85% rename from src/modules/actions/api/sessions.ts rename to src/util/sessions.ts index 992b87f70..0c89c1e94 100644 --- a/src/modules/actions/api/sessions.ts +++ b/src/util/sessions.ts @@ -1,12 +1,29 @@ import * as idb from 'idb-keyval'; -import { ApiSessionData } from '../../../api/types'; +import { ApiSessionData } from '../api/types'; -import { DEBUG, LEGACY_SESSION_KEY, SESSION_USER_KEY } from '../../../config'; -import * as cacheApi from '../../../util/cacheApi'; +import { DEBUG, LEGACY_SESSION_KEY, SESSION_USER_KEY } from '../config'; +import * as cacheApi from './cacheApi'; const DC_IDS = [1, 2, 3, 4, 5]; +export function hasStoredSession(withLegacy = false) { + if (withLegacy && localStorage.getItem(LEGACY_SESSION_KEY)) { + return true; + } + + const userAuthJson = localStorage.getItem(SESSION_USER_KEY); + if (!userAuthJson) return false; + + try { + const userAuth = JSON.parse(userAuthJson); + return Boolean(userAuth && userAuth.id && userAuth.dcID); + } catch (err) { + // Do nothing. + return false; + } +} + export function storeSession(sessionData: ApiSessionData, currentUserId?: number) { const { mainDcId, keys, hashes } = sessionData; @@ -32,21 +49,14 @@ export function clearStoredSession() { } export function loadStoredSession(): ApiSessionData | undefined { - const userAuthJson = localStorage.getItem(SESSION_USER_KEY); - if (!userAuthJson) return undefined; - - let mainDcId: number | undefined; - const keys: Record = {}; - const hashes: Record = {}; - - try { - const userAuth = JSON.parse(userAuthJson); - mainDcId = Number(userAuth.dcID); - } catch (err) { - // Do nothing. + if (!hasStoredSession()) { + return undefined; } - if (!mainDcId) return undefined; + const userAuth = JSON.parse(localStorage.getItem(SESSION_USER_KEY)!); + const mainDcId = Number(userAuth.dcID); + const keys: Record = {}; + const hashes: Record = {}; DC_IDS.forEach((dcId) => { try {