import type { NodeSavedSession, NodeSavedSessionStore, NodeSavedState, NodeSavedStateStore, } from "@atproto/oauth-client-node"; import type { DrizzleClient } from "../drizzle/db"; import { eq } from "drizzle-orm"; import { sessions, states } from "../drizzle/schema"; export class StateStore implements NodeSavedStateStore { constructor(private db: DrizzleClient) {} async get(key: string): Promise { const result = await this.db.query.states.findFirst({ where: eq(states.key, key), }); if (!result?.state) return; return JSON.parse(result.state); } async set(key: string, value: NodeSavedState) { const state = JSON.stringify(value); await this.db .insert(states) .values({ key, state, }) .onConflictDoUpdate({ target: states.key, set: { state, }, }); } async del(key: string) { await this.db.delete(states).where(eq(states.key, key)); } } export class SessionStore implements NodeSavedSessionStore { constructor(private db: DrizzleClient) {} async get(key: string): Promise { const result = await this.db.query.sessions.findFirst({ where: eq(states.key, key), }); if (!result?.session) return; return JSON.parse(result.session) as NodeSavedSession; } async set(key: string, value: NodeSavedSession) { const session = JSON.stringify(value); await this.db .insert(sessions) .values({ key, session, }) .onConflictDoUpdate({ target: sessions.key, set: { session, }, }); } async del(key: string) { await this.db.delete(sessions).where(eq(states.key, key)); } }