From c23752a28ee044a9a87ad61fa36d894d19c1f8b9 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 22 Mar 2024 13:05:57 +0100 Subject: [PATCH] Remove legacy sessions support (#4369) --- src/components/App.tsx | 2 +- src/config.ts | 1 - src/global/actions/api/initial.ts | 12 +------- src/global/actions/ui/initial.ts | 2 +- src/global/cache.ts | 2 +- src/util/sessions.ts | 48 ++----------------------------- src/util/websync.ts | 2 +- 7 files changed, 7 insertions(+), 62 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 44642114f..eb4b1f8e8 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -136,7 +136,7 @@ const App: FC = ({ activeKey = AppScreens.main; break; } - } else if (hasStoredSession(true)) { + } else if (hasStoredSession()) { page = 'main'; activeKey = AppScreens.main; } else if (hasPasscode) { diff --git a/src/config.ts b/src/config.ts index 117bcd2a5..c1519e5a4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,7 +33,6 @@ export const INACTIVE_MARKER = '[Inactive]'; export const DEBUG_PAYMENT_SMART_GLOCAL = false; export const SESSION_USER_KEY = 'user_auth'; -export const LEGACY_SESSION_KEY = 'GramJs:sessionId'; export const PASSCODE_CACHE_NAME = 'tt-passcode'; export const GLOBAL_STATE_CACHE_DISABLED = false; diff --git a/src/global/actions/api/initial.ts b/src/global/actions/api/initial.ts index 6bc2fb83a..83907591c 100644 --- a/src/global/actions/api/initial.ts +++ b/src/global/actions/api/initial.ts @@ -3,7 +3,6 @@ import { ManagementProgress } from '../../../types'; import { CUSTOM_BG_CACHE_NAME, - IS_TEST, LANG_CACHE_NAME, LOCK_SCREEN_ANIMATION_DURATION_MS, MEDIA_CACHE_NAME, @@ -18,9 +17,7 @@ import { unsubscribe } from '../../../util/notifications'; import { clearEncryptedSession, encryptSession, forgetPasscode } from '../../../util/passcode'; import { parseInitialLocationHash, resetInitialLocationHash, resetLocationHash } from '../../../util/routing'; import { - clearLegacySessions, clearStoredSession, - importLegacySession, loadStoredSession, storeSession, } from '../../../util/sessions'; @@ -39,12 +36,7 @@ import { addUsers, clearGlobalForLockScreen, updateManagementProgress, updatePasscodeSettings, } from '../../reducers'; -addActionHandler('initApi', async (global, actions): Promise => { - if (!IS_TEST) { - await importLegacySession(); - void clearLegacySessions(); - } - +addActionHandler('initApi', (global, actions): ActionReturnType => { const initialLocationHash = parseInitialLocationHash(); void initApi(actions.apiUpdate, { @@ -208,8 +200,6 @@ addActionHandler('reset', (global, actions): ActionReturnType => { void cacheApi.clear(`${langCachePrefix}${i === 0 ? '' : i}`); } - void clearLegacySessions(); - updateAppBadge(0); actions.initShared({ force: true }); diff --git a/src/global/actions/ui/initial.ts b/src/global/actions/ui/initial.ts index ac77c48f6..273bbbd3b 100644 --- a/src/global/actions/ui/initial.ts +++ b/src/global/actions/ui/initial.ts @@ -64,7 +64,7 @@ addActionHandler('switchMultitabRole', async (global, actions, payload): Promise storeSession(session, session.userId); } - if (hasStoredSession(true)) { + if (hasStoredSession()) { setupCaching(); } diff --git a/src/global/cache.ts b/src/global/cache.ts index 8fef9b36e..e70f662a7 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -86,7 +86,7 @@ export function loadCache(initialState: GlobalState): GlobalState | undefined { const cache = readCache(initialState); - if (cache.passcode.hasPasscode || hasStoredSession(true)) { + if (cache.passcode.hasPasscode || hasStoredSession()) { setupCaching(); return cache; diff --git a/src/util/sessions.ts b/src/util/sessions.ts index fad647c9d..564551fde 100644 --- a/src/util/sessions.ts +++ b/src/util/sessions.ts @@ -1,19 +1,12 @@ -import * as idb from 'idb-keyval'; - import type { ApiSessionData } from '../api/types'; import { - DEBUG, GLOBAL_STATE_CACHE_KEY, LEGACY_SESSION_KEY, SESSION_USER_KEY, + DEBUG, GLOBAL_STATE_CACHE_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; - } - +export function hasStoredSession() { if (checkSessionLocked()) { return true; } @@ -102,43 +95,6 @@ export function loadStoredSession(): ApiSessionData | undefined { }; } -export async function importLegacySession() { - const sessionId = localStorage.getItem(LEGACY_SESSION_KEY); - if (!sessionId) return; - - const sessionJson = await idb.get(`GramJs:${sessionId}`); - try { - const sessionData = JSON.parse(sessionJson) as ApiSessionData; - storeSession(sessionData); - } catch (err) { - if (DEBUG) { - // eslint-disable-next-line no-console - console.warn('Failed to load legacy session', err); - } - } -} - -// Remove previously created IndexedDB and cache API sessions -export async function clearLegacySessions() { - try { - localStorage.removeItem(LEGACY_SESSION_KEY); - - const idbKeys = await idb.keys(); - - await Promise.all>([ - cacheApi.clear('GramJs'), - ...idbKeys - .filter((k) => typeof k === 'string' && k.startsWith('GramJs:GramJs-session-')) - .map((k) => idb.del(k)), - ]); - } catch (err) { - if (DEBUG) { - // eslint-disable-next-line no-console - console.warn('Failed to clear legacy session', err); - } - } -} - export function importTestSession() { const sessionJson = process.env.TEST_SESSION!; try { diff --git a/src/util/websync.ts b/src/util/websync.ts index 1d5bde4ff..428af2306 100644 --- a/src/util/websync.ts +++ b/src/util/websync.ts @@ -92,7 +92,7 @@ export function startWebsync() { lastTimeout = setTimeout(() => { const { authState } = getGlobal(); - const authed = authState === 'authorizationStateReady' || hasStoredSession(true); + const authed = authState === 'authorizationStateReady' || hasStoredSession(); forceWebsync(authed); }, Math.max(0, timeout * 1000)); }