Merge pull request #836 from vector-im/fix-779

Clear keys from local storage when logging out
This commit is contained in:
R Midhun Suresh 2022-08-24 19:43:02 +05:30 committed by GitHub
commit db62427342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -16,11 +16,12 @@ limitations under the License.
import {IDOMStorage} from "./types"; import {IDOMStorage} from "./types";
import {Storage} from "./Storage"; import {Storage} from "./Storage";
import { openDatabase, reqAsPromise } from "./utils"; import {openDatabase, reqAsPromise} from "./utils";
import { exportSession, importSession, Export } from "./export"; import {exportSession, importSession, Export} from "./export";
import { schema } from "./schema"; import {schema} from "./schema";
import { detectWebkitEarlyCloseTxnBug } from "./quirks"; import {detectWebkitEarlyCloseTxnBug} from "./quirks";
import { ILogItem } from "../../../logging/types"; import {ILogItem} from "../../../logging/types";
import {clearKeysFromLocalStorage} from "./stores/SessionStore";
const sessionName = (sessionId: string) => `hydrogen_session_${sessionId}`; const sessionName = (sessionId: string) => `hydrogen_session_${sessionId}`;
const openDatabaseWithSessionId = function(sessionId: string, idbFactory: IDBFactory, localStorage: IDOMStorage, log: ILogItem) { const openDatabaseWithSessionId = function(sessionId: string, idbFactory: IDBFactory, localStorage: IDOMStorage, log: ILogItem) {
@ -79,6 +80,7 @@ export class StorageFactory {
delete(sessionId: string): Promise<IDBDatabase> { delete(sessionId: string): Promise<IDBDatabase> {
const databaseName = sessionName(sessionId); const databaseName = sessionName(sessionId);
clearKeysFromLocalStorage(this._localStorage, databaseName);
const req = this._idbFactory.deleteDatabase(databaseName); const req = this._idbFactory.deleteDatabase(databaseName);
return reqAsPromise(req); return reqAsPromise(req);
} }

View File

@ -24,6 +24,23 @@ export interface SessionEntry {
value: any; 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 { export class SessionStore {
private _sessionStore: Store<SessionEntry> private _sessionStore: Store<SessionEntry>
private _localStorage: IDOMStorage; private _localStorage: IDOMStorage;
@ -34,7 +51,7 @@ export class SessionStore {
} }
private get _localStorageKeyPrefix(): string { private get _localStorageKeyPrefix(): string {
return `${this._sessionStore.databaseName}.session.`; return getLocalStorageKeyPrefix(this._sessionStore.databaseName);
} }
async get(key: string): Promise<any> { async get(key: string): Promise<any> {