// The {did, handle, avatar} mirror of the OAuth session, kept in // storage.local for contexts that cannot run the OAuth client (the worker) or // want it without one (the popup on open). Separate from oauth.ts so the // worker can import it without pulling in the browser OAuth client — and // because the offscreen document that runs that client on Chrome cannot write // the mirror itself: offscreen documents get no extension storage at all. import { ext } from './platform' import type { SessionInfo } from './types' export const SUB_KEY = 'oauth.sub' export const SESSION_KEY = 'session' /** * The last signed-in handle, kept separately so the sign-in form can be * prefilled when the session expires and the mirror is gone. Cleared on a * deliberate sign-out; left in place when the session dies on its own. */ export const LAST_HANDLE_KEY = 'lastHandle' /** * storage.local marker set when a session is dropped without the user asking * (refresh token expired or revoked remotely). It stays until a fresh sign-in * retires it, so the popup can explain why the user is signed out and the * toolbar warning survives closing the popup. */ export const SESSION_EXPIRED_KEY = 'sessionExpired' export async function getStoredSession(): Promise { const stored = await ext.storage.local.get(SESSION_KEY) return stored[SESSION_KEY] as SessionInfo | undefined } /** A fresh sign-in also retires any pending expiry note. */ export async function saveSessionMirror(info: SessionInfo): Promise { const mirror: Record = { [SUB_KEY]: info.did, [SESSION_KEY]: info } if (info.handle) mirror[LAST_HANDLE_KEY] = info.handle await ext.storage.local.set(mirror) await ext.storage.local.remove(SESSION_EXPIRED_KEY) } /** Deliberate sign-out: drop everything, including any expiry note. */ export async function clearSessionMirror(): Promise { await ext.storage.local.remove([SUB_KEY, SESSION_KEY, SESSION_EXPIRED_KEY, LAST_HANDLE_KEY]) } /** The session died on its own: drop it but leave the expiry note. */ export async function markSessionExpired(): Promise { await ext.storage.local.remove([SUB_KEY, SESSION_KEY]) await ext.storage.local.set({ [SESSION_EXPIRED_KEY]: true }) }