@coolgk/token

an expirable, revocable, renewable token with data storage

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
214May 9, 2021Nov 18, 2017Minified + gzip package size for @coolgk/token in KB

Readme

@coolgk/token

a javascript / typescript module

npm install @coolgk/token

an expirable, revocable, renewable token with data storage

Report bugs here: https://github.com/coolgk/node-utils/issues

Examples

import { Token } from '@coolgk/token';
import { createClient } from 'redis';
// OR
// const { Token } = require('@coolgk/token');
// const createClient = require('redis').createClient;

(async () => {

    const redisClient = createClient({
        host: 'localhost',
        port: 6379,
        password: '----'
    });

    const token = new Token({
        redisClient: redisClient,
        expiry: 5,
        token: 'abcde'
    });

    console.log(
        await token.verify()
    ) // false

    await token.renew();

    console.log(
        await token.verify()
    ) // true

    console.log(
        await token.get('var1');
    ); // null

    console.log(
        await token.getAll()
    ); // {}

    await token.set('var1', {a: 'var1', b: false});

    console.log(
        await token.get('var1');
    ); // {a: 'var1', b: false}

    await token.set('var2', 'string var 2');

    console.log(
        await token.getAll()
    ); // { var1: { a: 'var1', b: false }, var2: 'string var 2' }

    await token.delete('var2');

    console.log(
        await token.get('var2');
    ); // null

    console.log(
        await token.getAll()
    ); // { var1: { a: 'var1', b: false } }

    await token.destroy();

    console.log(
        await token.verify()
    ) // false

    console.log(
        await token.get('var1');
    ); // null

    console.log(
        await token.getAll()
    ); // {}

    redisClient.quit();
})()

Classes

Token

Constants

TokenError : object

Error Codes

Token

Kind: global class

new Token(options)

Param Type Default Description
options object
options.token string token string for creating a token object
options.redisClient object redis client from redis.createClient()
[options.prefix] string "'token'" prefix used in redis e.g. token:[TOKEN_STRING...]
[options.expiry] number 0 in seconds. 0 = never expire

token.renew([expiry]) ⇒ promise

Kind: instance method of Token

Param Type Description
[expiry] number in seconds

token.set(name, value) ⇒ promise

set a data field value

Kind: instance method of Token

Param Type Description
name string field name
value * anything can be JSON.stringify'ed

token.verify() ⇒ promise.<boolean>

verify if token has expired

Kind: instance method of Token

token.get(name) ⇒ promise

get the value of a data field

Kind: instance method of Token

Param Type Description
name string data field name

token.destroy() ⇒ promise

delete the token

Kind: instance method of Token

token.delete(name) ⇒ promise

delete a data field in the token

Kind: instance method of Token

Param Type Description
name string data field name

token.getAll() ⇒ promise.<{}>

get the values of all data fields in the token

Kind: instance method of Token

token.setToken(token)

set a new token string

Kind: instance method of Token

Param Type Description
token string new token string

TokenError : object

Error Codes

Kind: global constant
Properties

Name Type Description
INVALID_TOKEN string invalid token string
RESERVED_NAME string reserved names are used when setting token variables e.g. _timestamp
EXPIRED_TOKEN string token expired or renew() has not been called

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.