From fafe478e5ce5175387d2c46d21a31963edd2ab33 Mon Sep 17 00:00:00 2001 From: nimbleghost <132819643+nimbleghost@users.noreply.github.com> Date: Sat, 17 Jun 2023 22:08:25 +0200 Subject: [PATCH] Sync localStorage to indexedDB on startup --- web/src/app/Session.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/web/src/app/Session.js b/web/src/app/Session.js index 82249b06..7464150c 100644 --- a/web/src/app/Session.js +++ b/web/src/app/Session.js @@ -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) {