a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add uncommited agent file

+41
+41
app/api/bsky/agent.ts
··· 1 + import { Agent } from "@atproto/api"; 2 + import { cookies } from "next/headers"; 3 + import { createOauthClient } from "src/atproto-oauth"; 4 + import { supabaseServerClient } from "supabase/serverClient"; 5 + 6 + export async function getAuthenticatedAgent(): Promise<Agent | null> { 7 + try { 8 + const cookieStore = await cookies(); 9 + const authToken = 10 + cookieStore.get("auth_token")?.value || 11 + cookieStore.get("external_auth_token")?.value; 12 + 13 + if (!authToken || authToken === "null") return null; 14 + 15 + const { data } = await supabaseServerClient 16 + .from("email_auth_tokens") 17 + .select("identities(atp_did)") 18 + .eq("id", authToken) 19 + .eq("confirmed", true) 20 + .single(); 21 + 22 + const did = data?.identities?.atp_did; 23 + if (!did) return null; 24 + 25 + const oauthClient = await createOauthClient(); 26 + const session = await oauthClient.restore(did); 27 + return new Agent(session); 28 + } catch (error) { 29 + console.error("Failed to get authenticated agent:", error); 30 + return null; 31 + } 32 + } 33 + 34 + export async function getAgent(): Promise<Agent> { 35 + const agent = await getAuthenticatedAgent(); 36 + if (agent) return agent; 37 + 38 + return new Agent({ 39 + service: "https://public.api.bsky.app", 40 + }); 41 + }