koa-jwt is a middleware for Koa.js applications designed to authenticate HTTP requests using JSON Web Tokens (JWTs). It parses and validates JWTs typically provided in the `Authorization` header, or optionally from a cookie or a custom `getToken` function. Upon successful validation, the decoded JWT payload is exposed on `ctx.state.user` (by default) for subsequent middleware to use for authorization and access control. The current stable version is 4.0.4. Releases are driven by dependency updates (especially `jsonwebtoken`) and bug fixes, with major versions tied to Node.js support or significant internal changes. It differentiates itself by providing a streamlined, Koa-idiomatic approach to JWT authentication, leveraging Koa's async/await middleware pattern, and integrates well with `koa-unless` for path-based exclusion. It supports single or multiple secrets, including rolling secrets or mixed authentication methods (e.g., Auth0 PEM files and shared secrets).
npm install koa-jwtVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `koa-jwt` to protect routes in a Koa application, including token generation, login, public and protected endpoints, and basic error handling.
Ensure your Node.js environment is version 8 or higher. For Koa 2 with Node < 7.6, use `koa-jwt@2`. For Koa 1, use `koa-jwt@1`.
Review your custom `getToken` or `isRevoked` implementations if they directly call `jsonwebtoken` functions. Ensure your Node.js environment meets `jsonwebtoken` v9's minimum requirements (Node.js >= 12).
Always set `debug: false` in production environments. Implement custom error handling middleware to provide generic 401 messages without exposing internal details. If `debug: true` is needed for development, ensure it's not enabled in deployed applications.
Be explicit about your token source. If using `opts.getToken`, ensure it correctly handles all expected scenarios and returns `null` if no token is found from its custom source, allowing fallback to cookie or header if desired. Otherwise, rely on the default order.
Be mindful of `ctx.state.secret`. If you intend to use a single secret, ensure no preceding middleware modifies `ctx.state.secret`. If you use dynamic secrets, carefully manage how `ctx.state.secret` is populated and its lifecycle.
Provide a `secret` string or buffer in the `koa-jwt` middleware options, e.g., `app.use(jwt({ secret: 'your-secret' }))`. Alternatively, if using dynamic secrets, ensure a preceding middleware correctly sets `ctx.state.secret`.The client needs to obtain a new, valid (unexpired) JWT from your authentication endpoint. On the server side, you can catch `TokenExpiredError` specifically in your error handling middleware to return a more informative response.
Ensure the `secret` used by `koa-jwt` on the server matches *exactly* the secret used to sign the token. Check for environmental variable mismatches, trimming issues, or different keys for different services.
For ESM, ensure you are using `import jwt from 'koa-jwt';`. For CommonJS, `const jwt = require('koa-jwt');` is correct. Avoid `import { jwt } from 'koa-jwt';` as it is a default export.