Fix showing Main layout for non-logged users

This commit is contained in:
Alexander Zinchuk 2021-06-23 16:29:17 +03:00
parent ae25c8723a
commit c6ed04fea6
6 changed files with 48 additions and 31 deletions

View File

@ -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<GlobalState, 'authState'>;
@ -55,9 +54,7 @@ const App: FC<StateProps & DispatchProps> = ({ authState, disconnect }) => {
}
}
const hasSession = localStorage.getItem(SESSION_USER_KEY) || localStorage.getItem(LEGACY_SESSION_KEY);
return hasSession ? renderMain() : <Auth />;
return hasStoredSession(true) ? renderMain() : <Auth />;
};
function renderMain() {

View File

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

View File

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

View File

@ -23,7 +23,7 @@ import {
importLegacySession,
clearLegacySessions,
importTestSession,
} from './sessions';
} from '../../../util/sessions';
addReducer('initApi', (global: GlobalState, actions) => {
(async () => {

View File

@ -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,
});
}

View File

@ -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<number, string> = {};
const hashes: Record<number, string> = {};
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<number, string> = {};
const hashes: Record<number, string> = {};
DC_IDS.forEach((dcId) => {
try {