the statusphere demo reworked into a vite/react app in a monorepo
at main 56 lines 1.7 kB view raw
1import type { 2 NodeSavedSession, 3 NodeSavedSessionStore, 4 NodeSavedState, 5 NodeSavedStateStore, 6} from '@atproto/oauth-client-node' 7 8import type { Database } from '#/db' 9 10export class StateStore implements NodeSavedStateStore { 11 constructor(private db: Database) {} 12 async get(key: string): Promise<NodeSavedState | undefined> { 13 const result = await this.db 14 .selectFrom('auth_state') 15 .selectAll() 16 .where('key', '=', key) 17 .executeTakeFirst() 18 if (!result) return 19 return JSON.parse(result.state) as NodeSavedState 20 } 21 async set(key: string, val: NodeSavedState) { 22 const state = JSON.stringify(val) 23 await this.db 24 .insertInto('auth_state') 25 .values({ key, state }) 26 .onConflict((oc) => oc.doUpdateSet({ state })) 27 .execute() 28 } 29 async del(key: string) { 30 await this.db.deleteFrom('auth_state').where('key', '=', key).execute() 31 } 32} 33 34export class SessionStore implements NodeSavedSessionStore { 35 constructor(private db: Database) {} 36 async get(key: string): Promise<NodeSavedSession | undefined> { 37 const result = await this.db 38 .selectFrom('auth_session') 39 .selectAll() 40 .where('key', '=', key) 41 .executeTakeFirst() 42 if (!result) return 43 return JSON.parse(result.session) as NodeSavedSession 44 } 45 async set(key: string, val: NodeSavedSession) { 46 const session = JSON.stringify(val) 47 await this.db 48 .insertInto('auth_session') 49 .values({ key, session }) 50 .onConflict((oc) => oc.doUpdateSet({ session })) 51 .execute() 52 } 53 async del(key: string) { 54 await this.db.deleteFrom('auth_session').where('key', '=', key).execute() 55 } 56}