Sync localStorage to indexedDB on startup

pull/751/head
nimbleghost 2023-06-17 22:08:25 +02:00
parent b7bb4459f9
commit fafe478e5c
1 changed files with 19 additions and 0 deletions

View File

@ -11,6 +11,25 @@ class Session {
kv: "&key",
});
this.db = db;
// existing sessions (pre-v2.6.0) haven't called `store` with the session-replica,
// so attempt to sync any values from localStorage to IndexedDB
if (typeof localStorage !== "undefined" && this.exists()) {
const username = this.username();
const token = this.token();
this.db.kv
.bulkPut([
{ key: "user", value: username },
{ key: "token", value: token },
])
.then(() => {
console.log("[Session] Synced localStorage session to IndexedDB", { username });
})
.catch((e) => {
console.error("[Session] Failed to sync localStorage session to IndexedDB", e);
});
}
}
async store(username, token) {