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
Supported JWT Algorithms
The algorithm option accepts any algorithm supported by hono/jwt:
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 - Stores auth data in the session via
setSession("rest", { userId, organizationId, ...claims }) - 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 getSession("rest") to access auth data:
Returns 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 session object.
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.