Session: Simplify storage (#5775)

This commit is contained in:
zubiden 2025-03-27 19:03:26 +01:00 committed by Alexander Zinchuk
parent aef7c39809
commit 6673937e24
4 changed files with 3 additions and 29 deletions

View File

@ -103,7 +103,6 @@ export interface ApiWebSession {
export interface ApiSessionData { export interface ApiSessionData {
mainDcId: number; mainDcId: number;
keys: Record<number, string | number[]>; keys: Record<number, string | number[]>;
hashes: Record<number, string | number[]>;
isTest?: true; isTest?: true;
} }

View File

@ -28,7 +28,6 @@ export default class CallbackSession extends MemorySession {
const { const {
mainDcId, mainDcId,
keys, keys,
hashes,
isTest, isTest,
} = this._sessionData; } = this._sessionData;
const { const {
@ -45,16 +44,8 @@ export default class CallbackSession extends MemorySession {
? Buffer.from(keys[dcId] as string, 'hex') ? Buffer.from(keys[dcId] as string, 'hex')
: Buffer.from(keys[dcId]); : Buffer.from(keys[dcId]);
if (hashes[dcId]) { this._authKeys[dcId] = new AuthKey();
const hash = typeof hashes[dcId] === 'string' await this._authKeys[dcId].setKey(key);
? Buffer.from(hashes[dcId] as string, 'hex')
: Buffer.from(hashes[dcId]);
this._authKeys[dcId] = new AuthKey(key, hash);
} else {
this._authKeys[dcId] = new AuthKey();
await this._authKeys[dcId].setKey(key);
}
})); }));
} }
@ -85,7 +76,6 @@ export default class CallbackSession extends MemorySession {
const sessionData: SessionData = { const sessionData: SessionData = {
mainDcId: this._dcId, mainDcId: this._dcId,
keys: {}, keys: {},
hashes: {},
isTest: this._isTestServer || undefined, isTest: this._isTestServer || undefined,
}; };
@ -97,7 +87,6 @@ export default class CallbackSession extends MemorySession {
if (!authKey?._key) return; if (!authKey?._key) return;
sessionData.keys[dcId] = authKey._key.toString('hex'); sessionData.keys[dcId] = authKey._key.toString('hex');
sessionData.hashes[dcId] = authKey._hash!.toString('hex');
}); });
return sessionData; return sessionData;

View File

@ -20,6 +20,5 @@ export type EntityLike =
export interface SessionData { export interface SessionData {
mainDcId: number; mainDcId: number;
keys: Record<number, string | number[]>; keys: Record<number, string | number[]>;
hashes: Record<number, string | number[]>;
isTest?: true; isTest?: true;
} }

View File

@ -28,7 +28,7 @@ export function hasStoredSession() {
export function storeSession(sessionData: ApiSessionData, currentUserId?: string) { export function storeSession(sessionData: ApiSessionData, currentUserId?: string) {
const { const {
mainDcId, keys, hashes, isTest, mainDcId, keys, isTest,
} = sessionData; } = sessionData;
localStorage.setItem(SESSION_USER_KEY, JSON.stringify({ localStorage.setItem(SESSION_USER_KEY, JSON.stringify({
@ -40,12 +40,6 @@ export function storeSession(sessionData: ApiSessionData, currentUserId?: string
Object.keys(keys).map(Number).forEach((dcId) => { Object.keys(keys).map(Number).forEach((dcId) => {
localStorage.setItem(`dc${dcId}_auth_key`, JSON.stringify(keys[dcId])); localStorage.setItem(`dc${dcId}_auth_key`, JSON.stringify(keys[dcId]));
}); });
if (hashes) {
Object.keys(hashes).map(Number).forEach((dcId) => {
localStorage.setItem(`dc${dcId}_hash`, JSON.stringify(hashes[dcId]));
});
}
} }
export function clearStoredSession() { export function clearStoredSession() {
@ -72,7 +66,6 @@ export function loadStoredSession(): ApiSessionData | undefined {
const mainDcId = Number(userAuth.dcID); const mainDcId = Number(userAuth.dcID);
const isTest = userAuth.test; const isTest = userAuth.test;
const keys: Record<number, string> = {}; const keys: Record<number, string> = {};
const hashes: Record<number, string> = {};
DC_IDS.forEach((dcId) => { DC_IDS.forEach((dcId) => {
try { try {
@ -80,11 +73,6 @@ export function loadStoredSession(): ApiSessionData | undefined {
if (key) { if (key) {
keys[dcId] = JSON.parse(key); keys[dcId] = JSON.parse(key);
} }
const hash = localStorage.getItem(`dc${dcId}_hash`);
if (hash) {
hashes[dcId] = JSON.parse(hash);
}
} catch (err) { } catch (err) {
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -99,7 +87,6 @@ export function loadStoredSession(): ApiSessionData | undefined {
return { return {
mainDcId, mainDcId,
keys, keys,
hashes,
isTest, isTest,
}; };
} }