Express JWT Permissions is an authorization middleware for Node.js applications, designed to work in conjunction with JWT authentication solutions like `express-jwt`. It inspects a decoded JWT token, typically found on `req.user` (or a configurable property), for a permissions array or a space-delimited scope string. The library, currently at stable version 1.3.7, has a moderate release cadence, primarily focusing on security updates, dependency bumps, and TypeScript typing enhancements. Its key differentiator lies in its flexible permission checking logic, supporting simple strings, arrays for AND logic, and nested arrays for complex OR logic combinations of permissions. It also provides configurable options for `requestProperty` and `permissionsProperty` to accommodate diverse JWT payload structures, moving beyond the default `req.user.permissions` pattern, and facilitates custom error handling for permission denials.
npm install express-jwt-permissionsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `express-jwt-permissions` with Express.js, including a mock `express-jwt` middleware, configurable permission checking, and a global error handler for `permission_denied` errors. It shows single, array (AND), and nested array (OR) permission logic.
Ensure compatibility by testing your application with `express-jwt-permissions@1.3.7`. Direct code changes within `express-jwt-permissions` usage are typically not required, but verify `express-unless` v2 changes do not impact your specific environment or deeper integrations.
Ensure `express-jwt` or a similar JWT decoding middleware is applied *before* any `express-jwt-permissions` middleware. Configure `express-jwt-permissions`'s `requestProperty` if your JWT middleware uses a different property than `req.user` (e.g., `req.auth`).
Initialize the guard with appropriate configuration options: `const guard = require('express-jwt-permissions')({ requestProperty: 'identity', permissionsProperty: 'scope' });` to match your JWT payload structure.Implement a dedicated error handling middleware *after* all routes and `express-jwt-permissions` checks: `app.use(function (err, req, res, next) { if (err.code === 'permission_denied') { res.status(403).send('Forbidden'); } next(err); });`Upgrade to `express-jwt-permissions@1.3.2` or higher to resolve typing compatibility issues, or ensure `esModuleInterop: true` is set in your `tsconfig.json` for older versions.
Implement a global Express error handler (after all routes) to specifically catch errors where `err.code === 'permission_denied'` and respond with an appropriate status (e.g., 403 Forbidden).
Verify that a JWT authentication middleware is running correctly *before* `express-jwt-permissions`. Ensure `guardFactory` is configured with `requestProperty` and `permissionsProperty` to match the actual location of permissions in your decoded JWT payload.
Ensure the guard factory is called to create an instance: `const guardFactory = require('express-jwt-permissions'); const guard = guardFactory();` or `import guardFactory from 'express-jwt-permissions'; const guard = guardFactory();`Augment the `express` Request type in a declaration file (e.g., `src/types/express.d.ts`): `declare namespace Express { interface Request { user?: { permissions?: string[] | string; [key: string]: any; }; auth?: { scope?: string | string[]; [key: string]: any; }; } }`. Adjust `user` to your `requestProperty` and `permissions` to your `permissionsProperty`.