From c37410642c4def393735720789bd9068aa5084e0 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Sat, 20 Dec 2025 09:02:09 +0400 Subject: [PATCH] add uncommited agent file --- app/api/bsky/agent.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/api/bsky/agent.ts diff --git a/app/api/bsky/agent.ts b/app/api/bsky/agent.ts new file mode 100644 index 00000000..3b015230 --- /dev/null +++ b/app/api/bsky/agent.ts @@ -0,0 +1,41 @@ +import { Agent } from "@atproto/api"; +import { cookies } from "next/headers"; +import { createOauthClient } from "src/atproto-oauth"; +import { supabaseServerClient } from "supabase/serverClient"; + +export async function getAuthenticatedAgent(): Promise { + try { + const cookieStore = await cookies(); + const authToken = + cookieStore.get("auth_token")?.value || + cookieStore.get("external_auth_token")?.value; + + if (!authToken || authToken === "null") return null; + + const { data } = await supabaseServerClient + .from("email_auth_tokens") + .select("identities(atp_did)") + .eq("id", authToken) + .eq("confirmed", true) + .single(); + + const did = data?.identities?.atp_did; + if (!did) return null; + + const oauthClient = await createOauthClient(); + const session = await oauthClient.restore(did); + return new Agent(session); + } catch (error) { + console.error("Failed to get authenticated agent:", error); + return null; + } +} + +export async function getAgent(): Promise { + const agent = await getAuthenticatedAgent(); + if (agent) return agent; + + return new Agent({ + service: "https://public.api.bsky.app", + }); +} -- 2.51.2