From bfd54f27644f5a1eb678e907be78fa1d471dd943 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 9 Aug 2022 23:11:02 +0530 Subject: [PATCH 1/3] Delete localstorage on logout --- src/matrix/Client.js | 6 ++++++ src/matrix/storage/idb/stores/SessionStore.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 44643cc1..438bf3ab 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -451,6 +451,12 @@ export class Client { async deleteSession(log) { if (this._sessionId) { + await log.wrap("sessionStore", async () => { + const storage = this._storage ?? await this._platform.storageFactory.create(this._sessionId, log); + const txn = await storage.readWriteTxn([storage.storeNames.session]); + txn.session.delete(); + storage.close(); + }); // need to dispose first, so the storage is closed, // and also first sync finishing won't call Session.start anymore, // which assumes that the storage works. diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts index 7faedc41..a7aaf594 100644 --- a/src/matrix/storage/idb/stores/SessionStore.ts +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -105,4 +105,17 @@ export class SessionStore { } this._sessionStore.delete(key); } + + delete(): void { + const keys: string[] = []; + for (let i = 0; i < localStorage.length; i++) { + const key = this._localStorage.key(i); + if (key?.startsWith(this._localStorageKeyPrefix)) { + keys.push(key); + } + } + for (const key of keys) { + this._localStorage.removeItem(key); + } + } } From 34dd7e4fa5f931a4fba85835660ec44e3307fe45 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 19 Aug 2022 16:31:48 +0530 Subject: [PATCH 2/3] Remove code --- src/matrix/Client.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 438bf3ab..44643cc1 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -451,12 +451,6 @@ export class Client { async deleteSession(log) { if (this._sessionId) { - await log.wrap("sessionStore", async () => { - const storage = this._storage ?? await this._platform.storageFactory.create(this._sessionId, log); - const txn = await storage.readWriteTxn([storage.storeNames.session]); - txn.session.delete(); - storage.close(); - }); // need to dispose first, so the storage is closed, // and also first sync finishing won't call Session.start anymore, // which assumes that the storage works. From d6cea6fc5c8fc4fd27c3bb848c8c6164652299fe Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 19 Aug 2022 16:36:21 +0530 Subject: [PATCH 3/3] Extract method to function outside class --- src/matrix/storage/idb/StorageFactory.ts | 12 ++++--- src/matrix/storage/idb/stores/SessionStore.ts | 32 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/matrix/storage/idb/StorageFactory.ts b/src/matrix/storage/idb/StorageFactory.ts index 1f64baf3..44d481eb 100644 --- a/src/matrix/storage/idb/StorageFactory.ts +++ b/src/matrix/storage/idb/StorageFactory.ts @@ -16,11 +16,12 @@ limitations under the License. import {IDOMStorage} from "./types"; import {Storage} from "./Storage"; -import { openDatabase, reqAsPromise } from "./utils"; -import { exportSession, importSession, Export } from "./export"; -import { schema } from "./schema"; -import { detectWebkitEarlyCloseTxnBug } from "./quirks"; -import { ILogItem } from "../../../logging/types"; +import {openDatabase, reqAsPromise} from "./utils"; +import {exportSession, importSession, Export} from "./export"; +import {schema} from "./schema"; +import {detectWebkitEarlyCloseTxnBug} from "./quirks"; +import {ILogItem} from "../../../logging/types"; +import {clearKeysFromLocalStorage} from "./stores/SessionStore"; const sessionName = (sessionId: string) => `hydrogen_session_${sessionId}`; const openDatabaseWithSessionId = function(sessionId: string, idbFactory: IDBFactory, localStorage: IDOMStorage, log: ILogItem) { @@ -79,6 +80,7 @@ export class StorageFactory { delete(sessionId: string): Promise { const databaseName = sessionName(sessionId); + clearKeysFromLocalStorage(this._localStorage, databaseName); const req = this._idbFactory.deleteDatabase(databaseName); return reqAsPromise(req); } diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts index a7aaf594..9ae9bb7e 100644 --- a/src/matrix/storage/idb/stores/SessionStore.ts +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -24,6 +24,23 @@ export interface SessionEntry { value: any; } +function getLocalStorageKeyPrefix(databaseName: string): string { + return `${databaseName}.session.`; +} + +export function clearKeysFromLocalStorage(localStorage: IDOMStorage, databaseName: string): void { + const keys: string[] = []; + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i); + if (key?.startsWith(getLocalStorageKeyPrefix(databaseName))) { + keys.push(key); + } + } + for (const key of keys) { + localStorage.removeItem(key); + } +} + export class SessionStore { private _sessionStore: Store private _localStorage: IDOMStorage; @@ -34,7 +51,7 @@ export class SessionStore { } private get _localStorageKeyPrefix(): string { - return `${this._sessionStore.databaseName}.session.`; + return getLocalStorageKeyPrefix(this._sessionStore.databaseName); } async get(key: string): Promise { @@ -105,17 +122,4 @@ export class SessionStore { } this._sessionStore.delete(key); } - - delete(): void { - const keys: string[] = []; - for (let i = 0; i < localStorage.length; i++) { - const key = this._localStorage.key(i); - if (key?.startsWith(this._localStorageKeyPrefix)) { - keys.push(key); - } - } - for (const key of keys) { - this._localStorage.removeItem(key); - } - } }