|
| 1 | +## Seamless Auth API Agent Guide |
| 2 | + |
| 3 | +This repository is an Express + TypeScript authentication service for passwordless auth flows. It supports WebAuthn/passkeys, OTP, magic links, JWKS publication, session management, admin endpoints, and a small system configuration layer persisted in Postgres. |
| 4 | + |
| 5 | +Use this file as the fast path. For the deeper architecture walkthrough, see [docs/architecture.md](/Users/brandoncorbett/git/seamless-auth-api/docs/architecture.md). |
| 6 | + |
| 7 | +## Start Here |
| 8 | + |
| 9 | +- Install dependencies: `npm install` |
| 10 | +- Run in development: `npm run dev` |
| 11 | +- Type-check/build: `npm run build` |
| 12 | +- Lint: `npm run lint` |
| 13 | +- Run tests once: `npm run test:run` |
| 14 | +- Coverage: `npm run coverage` |
| 15 | + |
| 16 | +The service starts from [src/server.ts](/Users/brandoncorbett/git/seamless-auth-api/src/server.ts), which initializes models, connects to the database, bootstraps `system_config`, builds the Express app, and begins listening. |
| 17 | + |
| 18 | +## Core Runtime Shape |
| 19 | + |
| 20 | +- [src/app.ts](/Users/brandoncorbett/git/seamless-auth-api/src/app.ts): global middleware, CORS, OpenAPI docs in dev, rate limiting, and top-level error handlers. |
| 21 | +- [src/lib/loadRoutes.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/loadRoutes.ts): dynamically imports every `*.routes.ts` file under `src/routes`. |
| 22 | +- [src/lib/createRouter.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/createRouter.ts): thin wrapper used by route modules. |
| 23 | +- [src/lib/defineRoute.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/defineRoute.ts): registers Express handlers and OpenAPI metadata, validates request payloads, and optionally validates JSON responses. |
| 24 | + |
| 25 | +When tracing behavior, start at the route file, then the controller, then the service/model/helper layers. |
| 26 | + |
| 27 | +## Important Folders |
| 28 | + |
| 29 | +- [src/routes](/Users/brandoncorbett/git/seamless-auth-api/src/routes): endpoint registration |
| 30 | +- [src/controllers](/Users/brandoncorbett/git/seamless-auth-api/src/controllers): request handling |
| 31 | +- [src/services](/Users/brandoncorbett/git/seamless-auth-api/src/services): session issuance, messaging, bootstrap promotion, auth event logging |
| 32 | +- [src/models](/Users/brandoncorbett/git/seamless-auth-api/src/models): Sequelize models |
| 33 | +- [src/config](/Users/brandoncorbett/git/seamless-auth-api/src/config): bootstrapped system config, required config metadata |
| 34 | +- [src/lib](/Users/brandoncorbett/git/seamless-auth-api/src/lib): routing, cookies, tokens, OpenAPI helpers |
| 35 | +- [src/middleware](/Users/brandoncorbett/git/seamless-auth-api/src/middleware): auth, rate limits, logging |
| 36 | +- [tests](/Users/brandoncorbett/git/seamless-auth-api/tests): unit, integration, e2e, and shared factories/setup |
| 37 | + |
| 38 | +## Auth Model |
| 39 | + |
| 40 | +There are three token states worth keeping straight: |
| 41 | + |
| 42 | +- Ephemeral token: short-lived pre-auth token used to continue registration/login flows. |
| 43 | +- Access token: signed JWT used for authenticated application access. |
| 44 | +- Refresh token: opaque random token stored hashed in the `sessions` table. |
| 45 | + |
| 46 | +Auth behavior depends on `AUTH_MODE`: |
| 47 | + |
| 48 | +- `web`: access/refresh/ephemeral tokens are primarily stored in cookies. |
| 49 | +- `server`: endpoints expect bearer tokens more often and return token payloads in JSON. |
| 50 | + |
| 51 | +Auth middleware is chosen centrally by [src/middleware/attachAuthMiddleware.ts](/Users/brandoncorbett/git/seamless-auth-api/src/middleware/attachAuthMiddleware.ts). |
| 52 | + |
| 53 | +## Config Model |
| 54 | + |
| 55 | +There are two config layers: |
| 56 | + |
| 57 | +- Environment variables: needed to boot the process and connect external systems. |
| 58 | +- `system_config` table: runtime configuration such as token TTLs, origins, roles, and WebAuthn RP settings. |
| 59 | + |
| 60 | +[src/config/bootstrapSystemConfig.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/bootstrapSystemConfig.ts) seeds missing `system_config` rows from env vars at startup. [src/config/getSystemConfig.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/getSystemConfig.ts) caches reads in-process, so remember to invalidate the cache after writes. |
| 61 | + |
| 62 | +## Messaging And Delivery |
| 63 | + |
| 64 | +OTP and magic link flows can operate in two modes: |
| 65 | + |
| 66 | +- Direct delivery: the API sends email/SMS itself via [src/services/messagingService.ts](/Users/brandoncorbett/git/seamless-auth-api/src/services/messagingService.ts). |
| 67 | +- External delivery: callers send header `x-seamless-auth-delivery-mode: external` and the API returns delivery payloads instead of sending them directly. |
| 68 | + |
| 69 | +Direct provider wiring currently lives in [src/config/directMessaging.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/directMessaging.ts). |
| 70 | + |
| 71 | +## Working Safely In This Repo |
| 72 | + |
| 73 | +- Prefer changing controllers/services over adding logic directly in routes. |
| 74 | +- When adding or updating routes, use `schemas`, not `schema`, so request parsing and OpenAPI generation both work. |
| 75 | +- If a route requires auth, prefer the `auth` option in `createRouter` definitions. That keeps middleware wiring and OpenAPI security metadata aligned. |
| 76 | +- If a route also needs admin checks or rate limiting, combine `auth` with extra `middleware`. |
| 77 | +- Be careful around cookie names and auth mode branching. Several flows have separate `web` and `server` response shapes. |
| 78 | +- Preserve existing local worktree changes unless the user explicitly asks you to clean them up. |
| 79 | + |
| 80 | +## Before You Finish A Change |
| 81 | + |
| 82 | +- Run `npm run build` |
| 83 | +- Run `npm run lint` |
| 84 | +- Run targeted tests for the touched area when practical |
| 85 | +- If you change request/response schemas or route auth, inspect the generated `/openapi.json` behavior in dev |
| 86 | + |
| 87 | +## Known Maintenance Traps |
| 88 | + |
| 89 | +- OpenAPI security metadata is only added when routes use the `auth` field in route definitions. Routes that manually call `attachAuthMiddleware(...)` can drift from the documented security model. |
| 90 | +- Cookie names in runtime code should be treated as the source of truth when checking auth behavior. |
| 91 | +- `system_config` values may come from the database even when `.env` changes, so config bugs are not always fixed by editing env vars alone. |
0 commit comments