From 570bed3010221393f7d3ee70ed85c4ef06c7cbc3 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 14 Jun 2021 16:01:12 +0300 Subject: [PATCH] Auth: Fixes for non-default DC users --- src/lib/gramjs/sessions/CallbackSession.js | 2 +- src/modules/actions/api/sessions.ts | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/gramjs/sessions/CallbackSession.js b/src/lib/gramjs/sessions/CallbackSession.js index d8add75c8..5e62f8bcb 100644 --- a/src/lib/gramjs/sessions/CallbackSession.js +++ b/src/lib/gramjs/sessions/CallbackSession.js @@ -89,7 +89,7 @@ class CallbackSession extends MemorySession { .keys(this._authKeys) .forEach((dcId) => { const authKey = this._authKeys[dcId]; - if (!authKey._key) return; + if (!authKey || !authKey._key) return; sessionData.keys[dcId] = authKey._key.toString('hex'); sessionData.hashes[dcId] = authKey._hash.toString('hex'); diff --git a/src/modules/actions/api/sessions.ts b/src/modules/actions/api/sessions.ts index 4d13605db..992b87f70 100644 --- a/src/modules/actions/api/sessions.ts +++ b/src/modules/actions/api/sessions.ts @@ -32,13 +32,22 @@ export function clearStoredSession() { } export function loadStoredSession(): ApiSessionData | undefined { - const legacySessionJson = localStorage.getItem(SESSION_USER_KEY); - if (!legacySessionJson) return undefined; + const userAuthJson = localStorage.getItem(SESSION_USER_KEY); + if (!userAuthJson) return undefined; - const { dcID: mainDcId } = JSON.parse(legacySessionJson); + let mainDcId: number | undefined; const keys: Record = {}; const hashes: Record = {}; + try { + const userAuth = JSON.parse(userAuthJson); + mainDcId = Number(userAuth.dcID); + } catch (err) { + // Do nothing. + } + + if (!mainDcId) return undefined; + DC_IDS.forEach((dcId) => { try { const key = localStorage.getItem(`dc${dcId}_auth_key`);