import type { Context } from "hono"; import { getCookie } from "hono/cookie"; import { getAgentForSession } from "./oauth"; import type { Agent } from "@atproto/api"; export interface Session { did: string | null; handle: string | null; agent: Agent | null; } export async function getSession(c: Context): Promise { const did = getCookie(c, "session"); if (!did) { return { did: null, handle: null, agent: null }; } try { const { agent, handle } = await getAgentForSession(did); return { did, handle, agent }; } catch (error) { // Session might be invalid or expired console.error("Session error:", error); return { did: null, handle: null, agent: null }; } } export function requireAuth(c: Context): Session { const session = c.get("session") as Session; if (!session.did || !session.agent) { throw new Error("Not authenticated"); } return session; }