Authentication Middleware
AuthMiddleware
Protects routes by requiring user authentication. Location:apps/web/app/auth/middleware/auth_middleware.ts
The URL to redirect to when authentication fails
handle()
handle()
Authenticates the user and denies access to unauthenticated requests.Parameters:
ctx- HTTP context objectnext- Next middleware functionoptions.guards- Optional array of authentication guards to use
- Stores return URL in session when unauthorized
- Supports multiple authentication guards
- Throws
E_UNAUTHORIZED_ACCESSon failure
Session key used to store the URL to return to after authentication
GuestMiddleware
Denies access to routes for authenticated users (e.g., login pages). Location:apps/web/app/auth/middleware/guest_middleware.ts
handle()
handle()
Redirects authenticated users away from guest-only pages.Parameters:
options.guards- Authentication guards to check (defaults to default guard)
- If user is authenticated, redirects to
afterAuthRedirectRoute - If user is not authenticated, allows request to proceed
SilentAuthMiddleware
Silently checks authentication status without blocking requests. Location:apps/web/app/auth/middleware/silent_auth_middleware.ts
handle()
handle()
Checks if user is authenticated without throwing errors.Features:
- Does not redirect or throw errors
- Populates
ctx.auth.userif authenticated - Sets
ctx.auth.userto null if not authenticated - Request continues regardless of authentication status
- Global middleware for all routes
- Optional authentication (user may or may not be logged in)
- Checking auth status for conditional UI rendering
Localization Middleware
DetectUserLocaleMiddleware
Detects and sets the user’s preferred language for i18n. Location:apps/web/app/core/middleware/detect_user_locale_middleware.ts
getRequestLocale()
getRequestLocale()
Determines the user’s language preference.Detection Priority:
X-User-Languageheaderuser-localecookieAccept-Languageheader- Default locale from config
handle()
handle()
Sets up i18n for the request.Features:
- Creates request-specific i18n instance
- Sets/updates
user-localecookie (30-day expiration) - Configures validation message provider
- Binds i18n to IoC container
- Shares i18n with Edge templates
- HttpOnly: true (prevents JavaScript access)
- Path:
/(available site-wide) - Max Age: 30 days
- SameSite: true (CSRF protection)
messagesProvider
messagesProvider
Configures i18n for validation messages.This enables automatic translation of validation error messages.
SwitchLocaleMiddleware
Manually switches the user’s language preference. Location:apps/web/app/common/middlewares/switch_locale_middleware.ts
handle()
handle()
Updates the locale cookie and redirects back.Parameters:
ctx.params.locale- New locale code from route parameter
- Sets
user-localecookie to new value - Redirects back to previous page
- Cookie persists for 30 days
Core Middleware
ContainerBindingsMiddleware
Binds request-specific values to the IoC container. Location:apps/web/app/core/middleware/container_bindings_middleware.ts
handle()
handle()
Registers context and logger in the container.Bindings:
HttpContext→ Current HTTP contextLogger→ Request-specific logger instance
InitializeBouncerMiddleware
Initializes authorization (Bouncer) for the request. Location:apps/web/app/core/middleware/initialize_bouncer_middleware.ts
handle()
handle()
Creates a Bouncer instance for authorization checks.Features:Usage in Controllers:Usage in Templates:
- Creates Bouncer instance with current user
- Loads all policies and abilities
- Sets container resolver for dependency injection
- Shares Bouncer helpers with Edge templates
bouncer property to HttpContext:
Middleware Stack
- Global Middleware
- Named Middleware
- Order of Execution
- Best Practices
Applied to all requests: