[WIP] book tracker on the atmosphere~
at main 1.1 kB view raw
1import type { RequestEvent } from '@sveltejs/kit'; 2import { Agent } from '@atproto/api'; 3import { getOAuthClient } from './atproto/client'; 4 5export const sessionCookieName = 'atproto-session'; 6 7export async function getSessionAgent(event: RequestEvent) { 8 const sessionDid = event.cookies.get(sessionCookieName); 9 if (!sessionDid) return null; 10 11 try { 12 const oauthClient = await getOAuthClient(); 13 const oauthSession = await oauthClient.restore(sessionDid); 14 return oauthSession ? new Agent(oauthSession) : null; 15 } catch (err) { 16 console.warn('OAuth restore failed:', err); 17 event.cookies.delete(sessionCookieName, { path: '/' }); 18 return null; 19 } 20} 21 22export function setSessionCookie(event: RequestEvent, did: string) { 23 event.cookies.set(sessionCookieName, did, { 24 path: '/', 25 httpOnly: true, 26 secure: process.env.NODE_ENV === 'production', 27 sameSite: 'lax', 28 maxAge: 60 * 60 * 24 * 30 // 30 days 29 }); 30} 31 32export function deleteSessionCookie(event: RequestEvent) { 33 event.cookies.delete(sessionCookieName, { 34 path: '/' 35 }); 36}