npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-jwt

v1.4.0

Published

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

Downloads

44

Readme

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: false // Enable notify-keyspace-events KEA
});

r.sign('507f191e810c19729de860ea').then(token => {
    r.verify(token).then(decode => {
    // [Object]
    }).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',
          dataToken: { // Public
              hello: 'world'
          },
          dataSession: { // Private
              hello: 'world',
              headers : req.headers
          }
        }
    }).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(decode =>
         // here you can get user from DB by id (decode.id)
         req.user = decode;
         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, 10 minutes, 5 hours, 3 days, 1 year ...
r.sign('507f191e810c19729de860ea', {
      ttl: '15 minutes'
}).then...

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

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

// Example TTL + dataToken + dataSession
r.sign('507f191e810c19729de860ea', {
      ttl: '15 minutes',
      dataToken: {world: 'hello'},
      dataSession: {hello: 'world'}
}).then...

Verify


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

// Get data from redis
r.verify(token, true).then(decode => {
/*
{
 "rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx",
 "dataToken": [Object]
 "dataSession": [Object]  ----> get data session
 "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

Compile

npm run compile

Test

npm test

License

MIT © Leonardo Rico