Registry / security / jwt-redis-session

jwt-redis-session

JSON →
library1.0.5jsnpmunverified

Connect/Express middleware that provides JWT-based session management with Redis backing. Version 1.0.5 is the latest stable release; the package is in maintenance mode with no recent updates. Key differentiators: uses JWT for cookie-less clients, stores session data in Redis with TTL management, and allows custom JWT claims. Unlike express-session, it does not rely on cookies and can be used across services. The module reserves the 'jti' claim for Redis key mapping, and Redis handles session expiration rather than JWT TTL.

npm install jwt-redis-session
INSTALL
IMPORT
SIG · JWT-REDIS-SESSION
J
jwt-redis-session
securityjavascriptv1.0.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JWTRedisSession
import JWTRedisSession from 'jwt-redis-session';
const JWTRedisSession = require('jwt-redis-session');
The package exports a single function via module.exports; default import is correct in ESM. CommonJS require also works, but the import style is preferable in modern projects.
options object
const session = JWTRedisSession({ client: redisClient, secret: 'mysecret' });
const session = new JWTRedisSession({ ... });
JWTRedisSession is a factory function, not a constructor. Do not use 'new'.
requestArg usage
// JWT is read from req.query.accessToken, req.body.accessToken, or x-access-token header (default option). app.use(JWTRedisSession({ requestArg: 'accessToken' }));
// Assuming requestArg is 'token' but expecting header 'x-token'; shorthand for camelBack: 'jwtToken' expects header 'x-jwt-token'
The header name is derived by converting requestArg to lowercase and prefixing 'x-'. For example, 'accessToken' yields 'x-access-token'.

Minimal Express app using jwt-redis-session with Redis for JWT-based session management.

const express = require('express'); const redis = require('redis'); const JWTRedisSession = require('jwt-redis-session'); const app = express(); const redisClient = redis.createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' }); app.use(require('body-parser').json()); app.use(JWTRedisSession({ client: redisClient, secret: process.env.JWT_SECRET || 'changeme', keyspace: 'sess:', maxAge: 86400, algorithm: 'HS256', requestKey: 'jwtSession', requestArg: 'jwtToken' })); app.post('/login', (req, res) => { // Simulate user login: store data in session req.jwtSession.user = { id: 1, username: 'test' }; req.jwtSession.create((err, token) => { if (err) return res.status(500).send(err); res.json({ token }); }); }); app.get('/protected', (req, res) => { // Session auto-populated from JWT in request if (!req.jwtSession.user) return res.status(401).send('Unauthorized'); res.json({ user: req.jwtSession.user }); }); app.listen(3000);
Debug
Known issues
breakingThe package is no longer maintained; last release was in 2016. It may have unpatched vulnerabilities (e.g., old JWT library versions).
fix
Migrate to actively maintained alternatives like express-session with JWT storage or a dedicated JWT middleware like 'jsonwebtoken' and 'redis'.
affects: <1.0.5
gotchaThe module looks for the JWT in query string, body, or header. If using body-parser, ensure body parsing is set up before this middleware, otherwise the JWT won't be found in req.body.
fix
Place body-parser middleware before jwt-redis-session: app.use(bodyParser.json()); app.use(JWTRedisSession({...}));
affects: >=1.0.0
gotchaWhen no JWT is present, the middleware creates an empty session object but does NOT call the create() method automatically. Developers must call req.session.create() to generate a new JWT.
fix
Explicitly call req.jwtSession.create(claims, callback) to produce a JWT token after setting session data.
affects: >=1.0.0
gotchaThe requestArg defaults to 'accessToken' which produces header 'x-access-token'. If you use a different requestArg, the header naming is lowercase with hyphen; e.g., 'myToken' becomes 'x-my-token'.
fix
Check the actual header name by converting: 'x-' + requestArg.replace(/([A-Z])/g, '-$1').toLowerCase()
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'create' of undefined
The session object is not attached to the request because the middleware was not applied or the requestKey option is misconfigured.
fix
Ensure app.use(JWTRedisSession(...)) is called with correct options, and use the correct requestKey (default 'session' but may be customized).
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
Redis server is not running or the client configuration is wrong.
fix
Start Redis server or set REDIS_URL environment variable to a valid Redis connection string. Provide a functional redisClient instance.
TypeError: secret must be a string or buffer
The 'secret' option is missing or is not a string.
fix
Provide a string secret: JWTRedisSession({ secret: 'your-secret-key', ... })
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies
redisrequiredRequired to create a Redis client for session storage; the module expects a pre-configured client.
expressoptionalCommonly used as Connect/Express middleware; while not a direct dependency, it is designed for Connect-compatible frameworks.
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
jwt-redis-session — npm install jwt-redis-session · libregistry