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,