Middleware
Middleware in pb-ext allows you to intercept and modify requests/responses, enforce authentication, log requests, and implement cross-cutting concerns.Middleware Patterns
pb-ext supports PocketBase’s middleware system with two binding methods:.Bind() - Hook Middleware
Binds a middleware that implementshook.Handler[T]:
- Returns
hook.Handler[T](wraps the event) - Can short-circuit the chain by not calling
e.Next() - Type-safe with generics
.BindFunc() - Function Middleware
Binds a plain function:- Simpler syntax
- No hook.Handler wrapper needed
- Can access
e.Router,e.App,e.Server
PocketBase Built-in Middleware
PocketBase provides several authentication middleware:RequireAuth
Requires any authenticated user (user or admin):RequireAdminAuth
Requires admin/superuser authentication:RequireRecordAuth
Requires authenticated record from a specific collection:RequireAdminOrRecordAuth
Requires either admin or record auth:Chaining Middleware
Middleware can be chained using multiple.Bind() calls:
Custom Middleware Examples
Request Logger Middleware
Fromcmd/server/routes.go:
- Calls
e.Next()to continue chain - Measures time before/after handler
- Returns the error from
e.Next()
Rate Limiting Middleware
Request ID Middleware
CORS Middleware
Validation Middleware
Error Recovery Middleware
Request Event Flow
The complete request flow with middleware: Example with multiple middleware layers:corsMiddleware(global)requestLoggerMW(route-specific)rateLimitMiddleware(route-specific)apis.RequireAuth()(route-specific)validateContentType(route-specific)createTodoHandler(handler)
Middleware with VersionedAPIRouter
When using pb-ext’s versioned router:Context Values
Store and retrieve values in the request context:Set Value in Middleware
Get Value in Handler
Best Practices
Call e.Next()
Always call
e.Next() to continue the middleware chain, unless intentionally short-circuiting.Order Matters
Place authentication middleware before authorization. Place logging middleware early to capture all requests.
Return Errors
Always return errors from middleware. Don’t silently swallow them.
Avoid Side Effects
Keep middleware focused. Avoid complex business logic in middleware.
Common Patterns
Conditional Middleware
Apply middleware only if a condition is met:Middleware Factory
Create reusable middleware with configuration:Response Transformation
Modify responses after handler execution:Performance Considerations
Middleware Cost
Each middleware adds latency:- Auth check: ~1-5ms (database lookup)
- Rate limiting: <1ms (in-memory check)
- Logging: <1ms (async preferred)
- Validation: ~1-10ms (depends on complexity)
Optimization Tips
- Cache auth results for repeated checks
- Use async logging to avoid blocking
- Skip middleware for health check endpoints
- Combine middleware when possible
Testing Middleware
Further Reading
- AST Parsing - Handler detection patterns
- Reserved Routes - pb-ext route middleware
- Reserved Collections - System collections