Do not destroy session on startup errors

This commit is contained in:
Alexander Zinchuk 2021-05-26 02:27:01 +03:00
parent 7b035dc1f9
commit f2e8977413
5 changed files with 9 additions and 24 deletions

View File

@ -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<GlobalState, 'authState' | 'authIsSessionRemembered'>;
type StateProps = Pick<GlobalState, 'authState'>;
type DispatchProps = Pick<GlobalActions, 'disconnect'>;
const App: FC<StateProps & DispatchProps> = ({ authState, authIsSessionRemembered, disconnect }) => {
const App: FC<StateProps & DispatchProps> = ({ authState, disconnect }) => {
const [isInactive, markInactive] = useFlag(false);
useEffect(() => {
@ -53,7 +53,7 @@ const App: FC<StateProps & DispatchProps> = ({ authState, authIsSessionRemembere
}
}
return authIsSessionRemembered ? renderMain() : <Auth />;
return localStorage.getItem(GRAMJS_SESSION_ID_KEY) ? renderMain() : <Auth />;
};
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);

View File

@ -103,7 +103,6 @@ function updateCache() {
'authState',
'authPhoneNumber',
'authRememberMe',
'authIsSessionRemembered',
'authNearestCountry',
'currentUserId',
'contactList',

View File

@ -71,7 +71,6 @@ export type GlobalState = {
authIsLoadingQrCode?: boolean;
authError?: string;
authRememberMe?: boolean;
authIsSessionRemembered?: boolean;
authNearestCountry?: string;
authIsCodeViaApp?: boolean;
authHint?: string;

View File

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

View File

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