Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// 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<SessionInfo | undefined> { 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<void> { const mirror: Record<string, unknown> = { [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<void> { 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<void> { await ext.storage.local.remove([SUB_KEY, SESSION_KEY]) await ext.storage.local.set({ [SESSION_EXPIRED_KEY]: true })}