Middleware, authentication, and authorization
Choose a route audience, read the authenticated principal, and enforce permissions in the operation.
Route audiences and middleware authenticate the caller. The business operation authorizes the action. Admin, member, and guest requests have separate authentication chains. Check resource ownership using the authenticated principal.
Let the route audience choose the authentication chain
After matching a route, Service wraps the handler in global, audience, module, and route middleware in that order. A middleware continues with next(). The current admin chain checks an Agent Bearer credential before administrator authentication. The member and guest audiences use their own authentication. Public routes can attach middleware where needed. The HTTP transport handles browser Origin checks.
| Caller | Credential to check |
|---|---|
| Signed-in Admin | Administrator session on the intended Service Origin |
| Trusted external tool | Agent Bearer credential intended for the management entry point |
| Member or guest | The credential for the corresponding audience |
A request with Bearer authentication uses that credential. Browser cookie authentication applies when Bearer is absent. The authenticated principal is available on ctx.state.auth. Use requirePrincipal(ctx, realm) when a specific identity is required.
Check operation-level permissions
After authenticating the caller, determine whether the principal can access the resource or perform the write. For a member editing their own profile, derive the account ID from the request, load the target, and verify ownership. Exercise an anonymous request, a credential for the wrong audience, a valid caller without access, and an authorized caller. Record status, error code, and request ID. Start tracing in packages/service/app/middlewares.ts and the feature controller. API reference · Request context