Abandon a session the rest of its install did not survive
This commit is contained in:
parent
795819c2ad
commit
2edd75e275
2 changed files with 25 additions and 4 deletions
|
|
@ -5,7 +5,7 @@ import type {Session} from "@welshman/app"
|
||||||
import {getTestEvents, maybeGetTestSession} from "@lib/test/session"
|
import {getTestEvents, maybeGetTestSession} from "@lib/test/session"
|
||||||
import {app, login, session} from "@app/core"
|
import {app, login, session} from "@app/core"
|
||||||
import {wallet} from "@app/lightning"
|
import {wallet} from "@app/lightning"
|
||||||
import {kv, ss, storage} from "@app/storage"
|
import {hasCachedIdentity, kv, ss, storage} from "@app/storage"
|
||||||
import {deactivateCurrentPomadeSession} from "@app/pomade"
|
import {deactivateCurrentPomadeSession} from "@app/pomade"
|
||||||
import {Push} from "@app/push"
|
import {Push} from "@app/push"
|
||||||
|
|
||||||
|
|
@ -58,11 +58,24 @@ const readLegacySession = async () => {
|
||||||
return legacy ? toCurrentSession(legacy) : undefined
|
return legacy ? toCurrentSession(legacy) : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const readStoredSession = async () => {
|
||||||
|
const session = (await ss.get<Session>("session")) ?? (await readLegacySession())
|
||||||
|
|
||||||
|
// A nip01 secret is the account rather than a credential, and a local signer can't stall startup.
|
||||||
|
if (session && session.method !== "nip01" && !(await hasCachedIdentity())) {
|
||||||
|
await ss.set("session", undefined)
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
// The session derives from the app's user, so read it back at startup and persist it on a change.
|
// The session derives from the app's user, so read it back at startup and persist it on a change.
|
||||||
export const restoreSession = async () => {
|
export const restoreSession = async () => {
|
||||||
// Test-only: an injected window.__TEST_SESSION__ wins over storage and is stripped from production.
|
// Test-only: an injected window.__TEST_SESSION__ wins over storage and is stripped from production.
|
||||||
const testSession = import.meta.env.DEV ? maybeGetTestSession() : undefined
|
const testSession = import.meta.env.DEV ? maybeGetTestSession() : undefined
|
||||||
const $session = testSession ?? (await ss.get<Session>("session")) ?? (await readLegacySession())
|
const $session = testSession ?? (await readStoredSession())
|
||||||
|
|
||||||
if ($session) {
|
if ($session) {
|
||||||
await login($session)
|
await login($session)
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,14 @@ const isExpired = (item: EventItem) =>
|
||||||
|
|
||||||
type PlaintextItem = {key: string; value: string}
|
type PlaintextItem = {key: string; value: string}
|
||||||
|
|
||||||
|
const DATABASE_PREFIX = "flotilla-9gl"
|
||||||
|
|
||||||
|
// indexeddb goes with the app container on every platform; the iOS keychain the session lives in does not.
|
||||||
|
export const hasCachedIdentity = async () =>
|
||||||
|
typeof indexedDB.databases === "function"
|
||||||
|
? (await indexedDB.databases()).some(db => db.name?.startsWith(DATABASE_PREFIX))
|
||||||
|
: true
|
||||||
|
|
||||||
/** Caches an app's repository, tracker and local collections in indexeddb, one database per identity. */
|
/** Caches an app's repository, tracker and local collections in indexeddb, one database per identity. */
|
||||||
class Storage {
|
class Storage {
|
||||||
ready: Promise<void>
|
ready: Promise<void>
|
||||||
|
|
@ -220,10 +228,10 @@ class Storage {
|
||||||
|
|
||||||
constructor(private readonly app: IApp) {
|
constructor(private readonly app: IApp) {
|
||||||
// Every identity used to share one database; drop it rather than leave it on disk.
|
// Every identity used to share one database; drop it rather than leave it on disk.
|
||||||
void deleteDB("flotilla-9gl")
|
void deleteDB(DATABASE_PREFIX)
|
||||||
|
|
||||||
this.pubkey = User.require(app).pubkey
|
this.pubkey = User.require(app).pubkey
|
||||||
this.db = new IDB({name: `flotilla-9gl-${this.pubkey}`, stores: TABLES})
|
this.db = new IDB({name: `${DATABASE_PREFIX}-${this.pubkey}`, stores: TABLES})
|
||||||
this.ready = this.start()
|
this.ready = this.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue