From 310cbc522911c987a3f730bc4218ff2f3725883d Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Sat, 20 Dec 2025 03:14:10 +0000 Subject: [PATCH] extract out agent function from bsky data routes --- app/api/bsky/quotes/route.ts | 42 +++--------------------------------------- app/api/bsky/thread/route.ts | 43 +++---------------------------------------- 2 file(s) changed, 6 insertion(s)(+), 79 deletion(s)(-) diff --git a/app/api/bsky/quotes/route.ts b/app/api/bsky/quotes/route.ts --- a/app/api/bsky/quotes/route.ts +++ b/app/api/bsky/quotes/route.ts @@ -1,38 +1,8 @@ -import { Agent, lexToJson } from "@atproto/api"; -import { cookies } from "next/headers"; +import { lexToJson } from "@atproto/api"; import { NextRequest } from "next/server"; -import { createOauthClient } from "src/atproto-oauth"; -import { supabaseServerClient } from "supabase/serverClient"; +import { getAgent } from "../agent"; export const runtime = "nodejs"; - -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 GET(req: NextRequest) { try { @@ -48,13 +18,7 @@ ); } - // Try to use authenticated agent if user is logged in, otherwise fall back to public API - let agent = await getAuthenticatedAgent(); - if (!agent) { - agent = new Agent({ - service: "https://public.api.bsky.app", - }); - } + const agent = await getAgent(); const response = await agent.app.bsky.feed.getQuotes({ uri, diff --git a/app/api/bsky/thread/route.ts b/app/api/bsky/thread/route.ts --- a/app/api/bsky/thread/route.ts +++ b/app/api/bsky/thread/route.ts @@ -1,39 +1,8 @@ -import { Agent, lexToJson } from "@atproto/api"; -import { ThreadViewPost } from "@atproto/api/dist/client/types/app/bsky/feed/defs"; -import { cookies } from "next/headers"; +import { lexToJson } from "@atproto/api"; import { NextRequest } from "next/server"; -import { createOauthClient } from "src/atproto-oauth"; -import { supabaseServerClient } from "supabase/serverClient"; +import { getAgent } from "../agent"; export const runtime = "nodejs"; - -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 GET(req: NextRequest) { try { @@ -49,13 +18,7 @@ ); } - // Try to use authenticated agent if user is logged in, otherwise fall back to public API - let agent = await getAuthenticatedAgent(); - if (!agent) { - agent = new Agent({ - service: "https://public.api.bsky.app", - }); - } + const agent = await getAgent(); const response = await agent.getPostThread({ uri, -- tangled.sh