mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
Merge pull request #836 from vector-im/fix-779
Clear keys from local storage when logging out
This commit is contained in:
commit
db62427342
@ -21,6 +21,7 @@ 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<IDBDatabase> {
|
||||
const databaseName = sessionName(sessionId);
|
||||
clearKeysFromLocalStorage(this._localStorage, databaseName);
|
||||
const req = this._idbFactory.deleteDatabase(databaseName);
|
||||
return reqAsPromise(req);
|
||||
}
|
||||
|
@ -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<SessionEntry>
|
||||
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<any> {
|
||||
|
Loading…
Reference in New Issue
Block a user