From f2e8977413d5b1a906a0681cccb262ded9d53d78 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 26 May 2021 02:27:01 +0300 Subject: [PATCH] Do not destroy session on startup errors --- src/App.tsx | 10 +++++----- src/global/cache.ts | 1 - src/global/types.ts | 1 - src/modules/actions/api/initial.ts | 5 ----- src/util/handleError.ts | 16 ++++------------ 5 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e72b669f..9615e22c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,7 +3,7 @@ import React, { withGlobal } from './lib/teact/teactn'; import { GlobalActions, GlobalState } from './global/types'; -import { INACTIVE_MARKER, PAGE_TITLE } from './config'; +import { GRAMJS_SESSION_ID_KEY, INACTIVE_MARKER, PAGE_TITLE } from './config'; import { pick } from './util/iteratees'; import { updateSizes } from './util/windowSize'; import { addActiveTabChangeListener } from './util/activeTabMonitor'; @@ -15,10 +15,10 @@ import Main from './components/main/Main.async'; import AppInactive from './components/main/AppInactive'; // import Test from './components/test/TestNoRedundancy'; -type StateProps = Pick; +type StateProps = Pick; type DispatchProps = Pick; -const App: FC = ({ authState, authIsSessionRemembered, disconnect }) => { +const App: FC = ({ authState, disconnect }) => { const [isInactive, markInactive] = useFlag(false); useEffect(() => { @@ -53,7 +53,7 @@ const App: FC = ({ authState, authIsSessionRemembere } } - return authIsSessionRemembered ? renderMain() : ; + return localStorage.getItem(GRAMJS_SESSION_ID_KEY) ? renderMain() : ; }; function renderMain() { @@ -65,6 +65,6 @@ function renderMain() { } export default withGlobal( - (global): StateProps => pick(global, ['authState', 'authIsSessionRemembered']), + (global): StateProps => pick(global, ['authState']), (setGlobal, actions): DispatchProps => pick(actions, ['disconnect']), )(App); diff --git a/src/global/cache.ts b/src/global/cache.ts index 60d3e8d3..c5b1449c 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -103,7 +103,6 @@ function updateCache() { 'authState', 'authPhoneNumber', 'authRememberMe', - 'authIsSessionRemembered', 'authNearestCountry', 'currentUserId', 'contactList', diff --git a/src/global/types.ts b/src/global/types.ts index a07be86e..8a462f05 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -71,7 +71,6 @@ export type GlobalState = { authIsLoadingQrCode?: boolean; authError?: string; authRememberMe?: boolean; - authIsSessionRemembered?: boolean; authNearestCountry?: string; authIsCodeViaApp?: boolean; authHint?: string; diff --git a/src/modules/actions/api/initial.ts b/src/modules/actions/api/initial.ts index 308ff842..12c59b44 100644 --- a/src/modules/actions/api/initial.ts +++ b/src/modules/actions/api/initial.ts @@ -20,11 +20,6 @@ addReducer('initApi', (global: GlobalState, actions) => { const sessionId = localStorage.getItem(GRAMJS_SESSION_ID_KEY) || undefined; void initApi(actions.apiUpdate, sessionId); - - return { - ...global, - authIsSessionRemembered: Boolean(sessionId), - }; }); addReducer('setAuthPhoneNumber', (global, actions, payload) => { diff --git a/src/util/handleError.ts b/src/util/handleError.ts index e17d42a3..96e6712e 100644 --- a/src/util/handleError.ts +++ b/src/util/handleError.ts @@ -1,6 +1,4 @@ -import { - DEBUG_ALERT_MSG, GLOBAL_STATE_CACHE_KEY, GRAMJS_SESSION_ID_KEY, -} from '../config'; +import { DEBUG_ALERT_MSG, GLOBAL_STATE_CACHE_KEY } from '../config'; import { throttle } from './schedulers'; window.addEventListener('error', handleErrorEvent); @@ -29,15 +27,9 @@ export function handleError(err: Error) { return; } - // For startup errors, we just clean the cache or the session and refresh the page. - if (Date.now() - startedAt <= STARTUP_TIMEOUT) { - if (localStorage.getItem(GLOBAL_STATE_CACHE_KEY)) { - localStorage.removeItem(GLOBAL_STATE_CACHE_KEY); - } else if (localStorage.getItem(GRAMJS_SESSION_ID_KEY)) { - localStorage.removeItem(GRAMJS_SESSION_ID_KEY); - } else { - return; - } + // For startup errors, we just clean the cache and refresh the page + if (Date.now() - startedAt <= STARTUP_TIMEOUT && localStorage.getItem(GLOBAL_STATE_CACHE_KEY)) { + localStorage.removeItem(GLOBAL_STATE_CACHE_KEY); isReloading = true; window.location.reload();