Monorepo for wisp.place. A static site hosting service built on top of the AT Protocol. wisp.place
1import { Did } from "@atproto/api"; 2import { NodeOAuthClient } from "@atproto/oauth-client-node"; 3import type { OAuthSession } from "@atproto/oauth-client-node"; 4import { Cookie } from "elysia"; 5 6 7export interface AuthenticatedContext { 8 did: Did; 9 session: OAuthSession; 10} 11 12export const authenticateRequest = async ( 13 client: NodeOAuthClient, 14 cookies: Record<string, Cookie<unknown>> 15): Promise<AuthenticatedContext | null> => { 16 try { 17 const did = cookies.did?.value as Did; 18 if (!did) return null; 19 20 const session = await client.restore(did, "auto"); 21 return session ? { did, session } : null; 22 } catch (err) { 23 console.error('Authentication error:', err); 24 return null; 25 } 26}; 27 28export const requireAuth = async ( 29 client: NodeOAuthClient, 30 cookies: Record<string, Cookie<unknown>> 31): Promise<AuthenticatedContext> => { 32 const auth = await authenticateRequest(client, cookies); 33 if (!auth) { 34 throw new Error('Authentication required'); 35 } 36 return auth; 37};