Something went wrong. Try again.
A decentralized music tracking and discovery platform built on AT Protocol 🎵 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { Agent, AtpAgent } from "@atproto/api";import { ctx } from "context";import { eq } from "drizzle-orm";import authSessions from "schema/auth-session";import extractPdsFromDid from "./extractPdsFromDid";import { env } from "./env";import { logger } from "logger";
export async function createAgent(did: string, handle: string): Promise<Agent> { const pds = await extractPdsFromDid(did); const agent = new AtpAgent({ service: new URL(pds), });
try { const [data] = await ctx.db .select() .from(authSessions) .where(eq(authSessions.key, did)) .execute();
if (!data) { throw new Error("No session found"); }
await agent.resumeSession(JSON.parse(data.session)); return agent; } catch (e) { logger.error`Resuming session ${did}`; await ctx.db .delete(authSessions) .where(eq(authSessions.key, did)) .execute();
await agent.login({ identifier: handle, password: env.ROCKSKY_PASSWORD, });
await ctx.db .insert(authSessions) .values({ key: did, session: JSON.stringify(agent.session), }) .onConflictDoUpdate({ target: authSessions.key, set: { session: JSON.stringify(agent.session) }, }) .execute();
logger.info`Logged in as ${handle}`;
return agent; }}