Authentication
Nile includes built-in JWT authentication via hono/jwt. Protected actions require a valid JWT token before the handler executes.
Configuration
Pass an auth object to your server config:
Auth Config Options
Protecting Actions
Set isProtected: true on any action that requires authentication:
When isProtected is true and auth is configured on the server:
- The engine extracts the JWT from the request (header or cookie)
- Verifies the token signature using
hono/jwt - Extracts
userIdandorganizationIdfrom the claims - Populates
context.authResultbefore the handler runs - If verification fails, the action returns an error without executing
Actions without isProtected (or with isProtected: false) skip auth entirely.
Accessing Auth Data
Inside any handler or hook, use the context accessors:
Both return undefined when no authentication occurred (e.g., unprotected actions).
JWT Claims Mapping
The JWT handler extracts identity fields from standard and common claim names:
All other claims are preserved in the claims object and spread into getUser().
Token Sources
Authorization Header (default)
Cookie
Custom Auth with Hooks
For auth logic beyond JWT (RBAC, API keys, OAuth sessions), use onBeforeActionHandler as a middleware gate:
The hook runs after JWT verification but before the action handler, giving you access to the verified user data for custom authorization logic.