瀏覽代碼

Sync localStorage to indexedDB on startup

nimbleghost 2 年之前
父節點
當前提交
fafe478e5c
共有 1 個文件被更改,包括 19 次插入0 次删除
  1. 19 0
      web/src/app/Session.js

+ 19 - 0
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) {