keepSession
uses node:fs
Keep a sign-in (and make it the one used last), or forget one (undefined for its DID).
import { readSessions, sessionsFile } from "#codebase/tcmts/atproto"; import type { OAuthSession } from "#codebase/tcmts/atproto"; import { randomBytes } from "node:crypto"; import fs from "node:fs"; import path from "node:path"; export function keepSession(did: string, s: OAuthSession | undefined): void { const kept = readSessions(); if (s) { kept.sessions[did] = s; kept.last = did; } else { delete kept.sessions[did]; if (kept.last === did) delete kept.last; } const file = sessionsFile(); // (its directory this user's alone, whatever made it; the file written new, never through a link someone left) fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 }); fs.chmodSync(path.dirname(file), 0o700); const tmp = `${file}.${process.pid}.${randomBytes(6).toString("hex")}`; fs.writeFileSync(tmp, `${JSON.stringify(kept, null, 2)}\n`, { mode: 0o600, flag: "wx" }); fs.renameSync(tmp, file); }