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 { GlobalActions, GlobalState } from './global/types';
import { import { INACTIVE_MARKER, PAGE_TITLE } from './config';
LEGACY_SESSION_KEY, INACTIVE_MARKER, SESSION_USER_KEY, PAGE_TITLE,
} from './config';
import { pick } from './util/iteratees'; import { pick } from './util/iteratees';
import { updateSizes } from './util/windowSize'; import { updateSizes } from './util/windowSize';
import { addActiveTabChangeListener } from './util/activeTabMonitor'; import { addActiveTabChangeListener } from './util/activeTabMonitor';
@ -15,6 +13,7 @@ import Auth from './components/auth/Auth';
import UiLoader from './components/common/UiLoader'; import UiLoader from './components/common/UiLoader';
import Main from './components/main/Main.async'; import Main from './components/main/Main.async';
import AppInactive from './components/main/AppInactive'; import AppInactive from './components/main/AppInactive';
import { hasStoredSession } from './util/sessions';
// import Test from './components/test/TestNoRedundancy'; // import Test from './components/test/TestNoRedundancy';
type StateProps = Pick<GlobalState, 'authState'>; 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 hasStoredSession(true) ? renderMain() : <Auth />;
return hasSession ? renderMain() : <Auth />;
}; };
function renderMain() { function renderMain() {

View File

@ -37,14 +37,15 @@ let client: TelegramClient;
let isConnected = false; let isConnected = false;
export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
onUpdate = _onUpdate;
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('>>> START INIT API'); console.log('>>> START INIT API');
} }
onUpdate = _onUpdate;
const { sessionData, userAgent } = initialArgs; const { sessionData, userAgent } = initialArgs;
const session = new sessions.CallbackSession(sessionData, onSessionUpdate);
client = new TelegramClient( client = new TelegramClient(
new sessions.CallbackSession(sessionData, onSessionUpdate), new sessions.CallbackSession(sessionData, onSessionUpdate),
@ -58,6 +59,8 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
} as any, } as any,
); );
onSessionUpdate(session.getSessionData());
client.addEventHandler(handleGramJsUpdate, gramJsUpdateEventBuilder); client.addEventHandler(handleGramJsUpdate, gramJsUpdateEventBuilder);
client.addEventHandler(updater, gramJsUpdateEventBuilder); client.addEventHandler(updater, gramJsUpdateEventBuilder);

View File

@ -11,13 +11,14 @@ import {
GLOBAL_STATE_CACHE_DISABLED, GLOBAL_STATE_CACHE_DISABLED,
GLOBAL_STATE_CACHE_KEY, GLOBAL_STATE_CACHE_KEY,
GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT,
LEGACY_SESSION_KEY, MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN,
MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN, GLOBAL_STATE_CACHE_USER_LIST_LIMIT, SESSION_USER_KEY, GLOBAL_STATE_CACHE_USER_LIST_LIMIT,
} from '../config'; } from '../config';
import { IS_SINGLE_COLUMN_LAYOUT } from '../util/environment'; import { IS_SINGLE_COLUMN_LAYOUT } from '../util/environment';
import { pick } from '../util/iteratees'; import { pick } from '../util/iteratees';
import { INITIAL_STATE } from './initial'; import { INITIAL_STATE } from './initial';
import { selectCurrentMessageList } from '../modules/selectors'; import { selectCurrentMessageList } from '../modules/selectors';
import { hasStoredSession } from '../util/sessions';
const UPDATE_THROTTLE = 1000; const UPDATE_THROTTLE = 1000;
@ -44,8 +45,7 @@ export function initCache() {
export function loadCache(initialState: GlobalState) { export function loadCache(initialState: GlobalState) {
if (!GLOBAL_STATE_CACHE_DISABLED) { if (!GLOBAL_STATE_CACHE_DISABLED) {
const hasSession = localStorage.getItem(SESSION_USER_KEY) || localStorage.getItem(LEGACY_SESSION_KEY); if (hasStoredSession(true)) {
if (hasSession) {
isAllowed = true; isAllowed = true;
addCallback(updateCacheThrottled); addCallback(updateCacheThrottled);
return readCache(initialState); return readCache(initialState);

View File

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

View File

@ -150,7 +150,8 @@ function onUpdateConnectionState(update: ApiUpdateConnectionState) {
} }
function onUpdateSession(update: ApiUpdateSession) { function onUpdateSession(update: ApiUpdateSession) {
if (!getGlobal().authRememberMe) { const { authRememberMe, authState } = getGlobal();
if (!authRememberMe || authState !== 'authorizationStateReady') {
return; return;
} }
@ -160,8 +161,14 @@ function onUpdateSession(update: ApiUpdateSession) {
} }
function onUpdateServerTimeOffset(update: ApiUpdateServerTimeOffset) { function onUpdateServerTimeOffset(update: ApiUpdateServerTimeOffset) {
const global = getGlobal();
if (global.serverTimeOffset === update.serverTimeOffset) {
return;
}
setGlobal({ setGlobal({
...getGlobal(), ...global,
serverTimeOffset: update.serverTimeOffset, serverTimeOffset: update.serverTimeOffset,
}); });
} }

View File

@ -1,12 +1,29 @@
import * as idb from 'idb-keyval'; 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 { DEBUG, LEGACY_SESSION_KEY, SESSION_USER_KEY } from '../config';
import * as cacheApi from '../../../util/cacheApi'; import * as cacheApi from './cacheApi';
const DC_IDS = [1, 2, 3, 4, 5]; 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) { export function storeSession(sessionData: ApiSessionData, currentUserId?: number) {
const { mainDcId, keys, hashes } = sessionData; const { mainDcId, keys, hashes } = sessionData;
@ -32,21 +49,14 @@ export function clearStoredSession() {
} }
export function loadStoredSession(): ApiSessionData | undefined { export function loadStoredSession(): ApiSessionData | undefined {
const userAuthJson = localStorage.getItem(SESSION_USER_KEY); if (!hasStoredSession()) {
if (!userAuthJson) return undefined; 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 (!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) => { DC_IDS.forEach((dcId) => {
try { try {