resumeSession

tcmts.atproto.resumeSession · #eskqyjq6 · function · runs anywhere

A kept sign-in that covers scope, as who (a handle or DID) or, without one, the one used last: its tokens refreshed if they've expired (and kept again). Undefined if there's none, or it can't be refreshed.

import { keptSession, refreshSession } from "#codebase/tcmts/atproto";
import type { Session, SessionStore } from "#codebase/tcmts/atproto";

export async function resumeSession(
  store: SessionStore,
  o: { who?: string; scope: string },
): Promise<Session | undefined> {
  const kept = store.read();
  const who = o.who?.trim().replace(/^@/, "").toLowerCase();
  const s = who
    ? Object.values(kept.sessions).find((x) => x.did === who || x.handle?.toLowerCase() === who)
    : kept.last
      ? kept.sessions[kept.last]
      : undefined;
  if (!s) return undefined;
  const covers =
    s.appPassword !== undefined || o.scope.split(" ").every((p) => s.scope.split(" ").includes(p));
  if (!covers) return undefined;
  if (s.expiresAt - 60_000 <= Date.now()) {
    if (!(await refreshSession(s))) return undefined;
  }
  store.keep(s.did, s);
  return keptSession(s, store);
}

uses

used by