@coolgk/token
v2.0.8
Published
an expirable, revocable, renewable token with data storage
Downloads
43
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
Constants
Token
Kind: global class
- Token
- new Token(options)
- .renew([expiry]) ⇒ promise
- .set(name, value) ⇒ promise
- .verify() ⇒ promise.<boolean>
- .get(name) ⇒ promise
- .destroy() ⇒ promise
- .delete(name) ⇒ promise
- .getAll() ⇒ promise.<{}>
- .setToken(token)
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 |