Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redis-jwt

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-jwt

Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user

  • 1.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

redis-jwt

NPM version Build Status dependencies Status devDependencies Status GitHub license

Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user

Requirements

  • Nodejs >= 6.x.x (Recommended 8.x.x)
  • Redis >= 3.x.x (Recommended 4.x.x)

Installation

Npm

npm install redis-jwt --save

Yarn

yarn add redis-jwt

Usage


import RedisJWT from 'redis-jwt';

const r = new RedisJWT({
	//host: '/tmp/redis.sock', //unix domain
	host: '127.0.0.1', //can be IP or hostname
	port: 6379, // port
	maxretries: 10, //reconnect retries, default 10
	//auth: '123', //optional password, if needed
	db: 0, //optional db selection
	secret: 'secret_key', // secret key for Tokens!
	multiple: false, // single or multiple sessions by user
	kea: true // Enable notify-keyspace-events KEA
});

r.sign('507f191e810c19729de860ea').then(token => {
	r.verify(token).then(result => {
		// object redis-jwt
	}).catch(err => {
		// Wrong token
	});
});

Example Redis-jwt with Express


import RedisJWT from 'redis-jwt';
import express from 'express';
const r = new RedisJWT();
const app = express();

// Login
app.get('/login', (req, res) => {
	r.sign('507f191e810c19729de860ea', {
        ttl: '15 minutes',
        data: {
            redis: {hello: 'world'},
            token: {world: 'hello'},
            request: req
		},
    }).then(token => {
		res.json({token});
  	});
});

// Me
app.get('/me', mw(), (req, res) => {
    res.json(req.user);
});

// Middleware
function mw() {
  return (req, res, next) => {
 	const token = req.headers['authorization'];
	r.verify(token).then(rjwt =>
		// here you can get user from DB by id (rjwt.id)
	    req.user = rjwt;
        next();
  	}).catch(err => {
        res.status(401).json({err})
  	})
  }
}

app.listen(3000, () => console.log('Server listening on port 3000!'));

Options

Sign


    // Basic
    r.sign('507f191e810c19729de860ea').then..

    // TTL : 50 seconds, 15 minutes, 5 hours, 3 days, 1 year ...
    r.sign('507f191e810c19729de860ea', {
    	ttl: '15 minutes'
    }).then...

    // Save data in redis : Object are saved in redis-jwt
    r.sign('507f191e810c19729de860ea', {
       data: {
          redis: {hello: 'world'}
	   }
    }).then...

    // Save data in token : Object are saved in token
    r.sign('507f191e810c19729de860ea', {
       data: {
	      token: {world: 'hello'}
	   }
    }).then...

	// Request : request are saved in redis-jwt (ip,agent)
    r.sign('507f191e810c19729de860ea', {
        data: {
          request: req
	    }
    }).then...

    // Example TTL + Data + Request
    r.sign('507f191e810c19729de860ea', {
        ttl: '15 minutes',
        data: {
           redis: {hello: 'world'},
           token: {world: 'hello'},
           request: req
        }
    }).then...

Verify


	// Basic
	r.verify(token).then(decode => {
		/*
		{
			"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx",
			"data": { token: [Object] }
			"iat": 1504334208,
			"id": "507f191e810c19729de860ea",
			"ttl": 60
		}
		*/
  	}).catch(err => {
		// Wrong token
  	})

	// Get data from redis
	r.verify(token, true).then(decode => {
		/*
		{
			"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx",
			"data": { token: [Object], redis: [Object], request: [Object] }
			"iat": 1504334208,
			"id": "507f191e810c19729de860ea",
			"ttl": 60
		}
		*/
  	}).catch(err => {
		// Wrong token
  	})

Exec


    // Execute Redis comands
    const exec = r.exec();
    
    exec.rawCall(['keys', `507f191e810c19729de860ea:*`], (err, result) => {
		/*
		[
			"507f191e810c19729de860ea:ZYYlwOGqTmx",
			"507f191e810c19729de860ea:d39K8J249Hd",
		]
		*/
    });

Call


    // Method's redis-jwt
    const call = r.call();

    // Test Ping
    call.ping().then..
    
     // Create
    call.create(key, value, ttl).then..
    
    // exits by key
    call.exists(key).then..
    
    // Get ttl by Key
    call.ttl(key).then..
    
    // Get values by key
    call.getValueByKey(key).then..
    
    // Get values by Pattern
    call.getValuesByPattern(pattern).then..
    
    // Get count by Pattern
    call.getCountByPattern(pattern).then..
    
    // Get info
    call.getInfo(section).then..
    
    // Destroy by key
    call.destroy(key).then..
    
    // Destroy multiple by key
    call.destroyMultiple(key).then..
    

Events


// Ready
r.on('ready', () => {
	console.log('redis-jwt-> ready!');
});

// connected
r.on('connected', () => {
	console.log('redis-jwt-> connected!');
});

// disconnected
r.on('disconnected', () => {
	console.log('redis-jwt-> disconnected!');
});

// error
r.on('error', (err) => {
	console.log('redis-jwt-> error!', err);
});

Development

Start

npm start

Serve

npm run serve

Build

npm run build

Test

npm test

License

MIT © Leonardo Rico

Keywords

FAQs

Package last updated on 07 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc