this repo has no description
1import type { Context } from "hono";
2import { getCookie } from "hono/cookie";
3import { getAgentForSession } from "./oauth";
4import type { Agent } from "@atproto/api";
5
6export interface Session {
7 did: string | null;
8 handle: string | null;
9 agent: Agent | null;
10}
11
12export async function getSession(c: Context): Promise<Session> {
13 const did = getCookie(c, "session");
14
15 if (!did) {
16 return { did: null, handle: null, agent: null };
17 }
18
19 try {
20 const { agent, handle } = await getAgentForSession(did);
21 return { did, handle, agent };
22 } catch (error) {
23 // Session might be invalid or expired
24 console.error("Session error:", error);
25 return { did: null, handle: null, agent: null };
26 }
27}
28
29export function requireAuth(c: Context): Session {
30 const session = c.get("session") as Session;
31 if (!session.did || !session.agent) {
32 throw new Error("Not authenticated");
33 }
34 return session;
35}