Selegic CRM Docs
ServerMiddleware & Lifecycle

Overview

Middleware responsibilities, HandlerContext lifecycle, and execution order.

Middleware in the CRM server is the first line of defense for every HTTP request. It builds the HandlerContext, enforces authentication, runs lifecycle hooks, and provides request-scoped services.

Key Responsibilities

  • Request Context: Add requestId, timing metadata
  • Session Resolution: Resolve user from cookies
  • Tenant Resolution: Resolve organization from header/API key
  • Authentication: Block anonymous requests where required
  • Lifecycle Hooks: Run before/after request hooks

Middleware Location

All middleware in lib/middleware/:

  • session.ts — Session resolution
  • tenant.ts — Tenant/org resolution
  • auth.ts — Route guards
  • lifecycle.ts — Lifecycle hooks

Quick Navigation

On this page