Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

session-token

0.5.2 • Public • Published

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

Keywords

install

npm i session-token

Downloadsweekly downloads

5

version

0.5.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability