// Signing in with atproto, and the one authenticated call this page makes. // // The page never sees a password: OAuth takes the operator to their own // account's server and brings back a DPoP-bound token. The record write goes // through the session's own fetch, which is what attaches it. import { BrowserOAuthClient } from "@atproto/oauth-client-browser"; import { putRecord } from "./atproto.js"; import { SERVER_NSID } from "./record.js"; export const HANDLE_RESOLVER = "https://public.api.bsky.app"; const LOOPBACK = ["localhost", "127.0.0.1", "[::1]"]; /** * Starts the OAuth client and picks up a session if one is already going — * including the one a sign-in redirect has just come back with. * * On a loopback origin the library synthesises development client metadata; * anywhere else the metadata has to be a real URL, which is what * public/client-metadata.json is for. */ export async function startSession() { const isLoopback = LOOPBACK.includes(location.hostname); const handleResolver = HANDLE_RESOLVER; const client = isLoopback ? new BrowserOAuthClient({ clientMetadata: undefined, handleResolver }) : await BrowserOAuthClient.load({ clientId: new URL("./client-metadata.json", location.href).href, handleResolver, }); const result = await client.init(); return { client, session: result?.session ?? null }; } /** * Writes the instance's dev.atfs.server record into the signed-in repo, at * the peer ID its identity derives. */ export function writeServerRecord(session, peerId, record) { return putRecord((path, init) => session.fetchHandler(path, init), { repo: session.did, collection: SERVER_NSID, rkey: peerId, record, }); }