AZ-005 — Users and session authentication API

Status

Done

Context

Read README, architecture, Docker, testing and CI before implementation. No user/auth tables/endpoints exist. backend/src/server.ts wires concrete repositories; app.ts accepts dependencies. packages/db owns Drizzle schema/migrations. AZ-003/004 provide safe codes/logging. The existing integration helper bypasses Drizzle's journal and naïvely splits SQL; auth migrations require exercising the actual migration mechanism. This task delivers backend login, not its UI.

Goal

Deliver persistent users and revocable cookie sessions with safe provisioning and coherent login/logout/current-user API behavior across host and packaged local environments.

Requirements

Identity, persistence and provisioning

Session and HTTP boundary

Migration and testing lifecycle

Non-goals

No frontend login yet, registration/invitations, admin UI/roles, OIDC provider, token refresh JWTs, email verification, recovery/change-password (later tasks), tenancy, or additional infrastructure.

Implementation guidance

Keep hashing/use-case policy in backend application code, HTTP/plugin adaptation in API code and PostgreSQL store queries behind repositories. server.ts composes them. Thin tooling wrappers belong within backend, not a new top-level package. Include .js import extensions per backend convention and TypeDoc comments for added intentional public API. Native Argon2 dependencies must work in the deployed pnpm --prod deploy --legacy Alpine image, proven by clean Docker build. Extend the route-surface contract test with controlled auth collaborators and await plugin readiness where necessary; conditional developer route surfaces are handled later, not hidden from OpenAPI.

Acceptance criteria

Verification

pnpm db:generate
pnpm quality
pnpm test
pnpm build
pnpm test:coverage
pnpm test:ci
pnpm validate:docker

Run generation only after schema changes; review generated SQL/journal without reformatting. Coverage needs local/test PostgreSQL. Docker validation includes integration once; upgrade/rerun tests must join that suite. Test introduced pnpm user:create and pnpm user:seed:test with dedicated synthetic targets and negative hosted-config cases. Manually inspect cookie flags and safe logs in local HTTP and a controlled secure-proxy test harness, not unauthorized live staging.

Documentation

Update OpenAPI, architecture (identity/session/CSRF boundaries), techstack, development (provisioning, secrets, exact origins, fixture flow), Docker (local versus hosted classification), testing (journaled migrations/session integration), CI (fixture inputs). Record an auth ADR with internal versus OIDC rationale and deployment limitations. Update current-state/handover/evidence.

Dependencies

AZ-004 (including AZ-001–003); existing Drizzle migration tooling, Fastify composition and local database/Compose/integration profiles.

Future considerations

Global user identity is not business ownership. A later multi-business epic must add scoped data access/membership decisions before admitting another business; no implicit global admin role now.