Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including session-token with all npm packages installed. Try it out:

var sessionToken = require("session-token")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

session-token v0.6.1

A library to create token in redis easily.

SessionToken

build status Test coverage David deps node version

NPM
A library to create token and save associated session data in redis easily.

1. Install

npm install session-token --save

2. How to use

First, you should have a redis server, because SessionToken save all the token data in redis. And then you can create a SessionToken object.

const SessionToken = require('session-token');
const Redis = require('ioredis');
const async = require('neo-async');
const redisClient = new Redis();//connect to the redis server of localhost:6379
const redisSub = new Redis();//the redis client for subscribe
const sessionToken = new SessionToken({
    expireTime:7200,//the time of seconds before the session data expired
    redisKeyPrefix:'myprefix:mytoken:',//the redis key's prefix
    redis:redisClient,//the redis client used to save session data
    subReis:redisSub,//The subscribe redis client to receive delete operation
    maxSize:1000000,// The max size of the cache in memory.
});

code 2.1

After creating the object of SessionToken, you can generate token and it will save the session data to the reids.

let token = null;
async.waterfall([
    function(next) {
        sessionToken.generate({name:'sunny',id:1},function(err,tokenViaCreate) {//save session
            if (err) {
                next(err);
                return console.error(err);
            }
            token = tokenViaCreate;
            next();
        });
    },
    function(next) {
        sessionToken.get(token,function(err,data) {//get session data via token
            if (err) {
                next(err);
                return console.error(err);
            }
            next();
        });
    }
]);

3. Api

See here.

4. License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free