An atproto based writing game loosely inspired by Fiasco!
AT Proto OAuth Authentication Implementation Plan#
Architecture Overview#
Combine patterns from both reference projects:
- statusphere: AT Proto OAuth client with DB-backed session/state storage
- solid-test: Cookie-based session management with vinxi/http useSession
- Our approach: AT Proto OAuth + iron-session cookies storing only session ID + full session data in DB
Database Schema Changes (src/db/schema.ts)#
- Add
auth_sessiontable (key: string PK, session: JSON/text) - Add
auth_statetable (key: string PK, state: JSON/text) - Add
usertable (did: string PK, handle: varchar, createdAt: timestamp) - Add
user_sessiontable (sessionId: string PK, did: string FK, createdAt: timestamp, expiresAt: timestamp)
New Files to Create#
src/auth/client.ts- NodeOAuthClient setupsrc/auth/storage.ts- StateStore & SessionStore classes (DB-backed)src/auth/session.ts- iron-session helper for cookie managementsrc/auth/index.ts- Export auth utilities
Server Actions (src/api/server.ts)#
initiateLogin(handle: string)- Start OAuth flowhandleOAuthCallback(params: URLSearchParams)- Complete OAuth, create user sessionlogout()- Destroy sessiongetUser()- Retrieve user from session
Routes#
- Update
src/routes/login.tsx- AT Proto handle input - Add OAuth callback route/handler
- Protect routes requiring auth
Environment Variables#
Add to .env:
COOKIE_SECRET- for iron-sessionPUBLIC_URL- for OAuth client metadata (optional, dev uses localhost)
Flow#
- User enters handle →
initiateLogin→ OAuth authorize URL - Redirect to PDS → User approves
- Callback →
handleOAuthCallback→ Store OAuth session in DB, create user_session, set cookie with sessionId - Cookie contains only sessionId → Server reads sessionId → Looks up user_session → Gets DID → Restores OAuth session from DB → Gets Agent