Use secure storage for session data

This commit is contained in:
Jon Staab
2026-03-23 17:51:51 -07:00
parent f5b1e91378
commit 610b8dd171
8 changed files with 100 additions and 4 deletions
+41
View File
@@ -1,4 +1,5 @@
import {call} from "@welshman/lib"
import {SecureStorage} from "@aparajita/capacitor-secure-storage"
import {Preferences} from "@capacitor/preferences"
import {IDB} from "@lib/indexeddb"
@@ -30,6 +31,46 @@ export const kv = call(() => {
return {get, set, clear}
})
export const ss = call(() => {
let p = Promise.resolve()
const get = async <T>(key: string): Promise<T | undefined> => {
let value = await SecureStorage.getItem(key)
if (!value) {
const legacy = await Preferences.get({key})
if (legacy.value) {
value = legacy.value
await SecureStorage.setItem(key, legacy.value)
await Preferences.remove({key})
}
}
if (!value) return undefined
try {
return JSON.parse(value)
} catch (e) {
return undefined
}
}
const set = async <T>(key: string, value: T): Promise<void> => {
p = p.then(() => SecureStorage.setItem(key, JSON.stringify(value)))
await p
}
const clear = async () => {
p = p.then(() => SecureStorage.clear())
await p
}
return {get, set, clear}
})
export const db = new IDB({
name: "flotilla-9gl",
version: 1,
+2 -1
View File
@@ -1,4 +1,4 @@
import {kv, db} from "@app/core/storage"
import {db, kv, ss} from "@app/core/storage"
import {Push} from "@app/util/notifications"
import {deactivateCurrentPomadeSession} from "@app/util/pomade"
@@ -6,6 +6,7 @@ export const logout = async () => {
await deactivateCurrentPomadeSession()
await Push.disable()
await kv.clear()
await ss.clear()
await db.clear()
localStorage.clear()
+3 -3
View File
@@ -27,7 +27,7 @@
import {setupHistory} from "@app/util/history"
import {setupAnalytics} from "@app/util/analytics"
import {authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy} from "@app/util/policies"
import {kv, db} from "@app/core/storage"
import {db, kv, ss} from "@app/core/storage"
import {device, userSettingsValues, notificationSettings, pushState} from "@app/core/state"
import {syncApplicationData} from "@app/core/sync"
import * as commands from "@app/core/commands"
@@ -96,7 +96,7 @@
const unsubscribe = call(async () => {
const unsubscribers: Unsubscriber[] = []
// Sync stuff to localstorage
// Sync stuff to storage
await Promise.all([
sync({
key: "device",
@@ -111,7 +111,7 @@
sync({
key: "sessions",
store: sessions,
storage: kv,
storage: ss,
}),
sync({
key: "shouldUnwrap",