A simple wrapper for a simpler JWT surface API
This wrapper is directly usable with Knifecycle.
Instantiate the JWT service
Kind: global function
Returns: Promise.<Object>
- A promise of the jwt service
Param | Type | Description |
---|---|---|
services | Object |
The services to inject |
services.JWT | function |
The JWT service configuration object |
[services.log] | function |
A logging function |
[services.time] | function |
A function returning the current timestamp |
Example
import initJWTService from 'jwt-service';
const jwt = await initJWTService({
JWT: {
secret: 'secret',
duration: '2d',
tolerance: '2h',
algorithms: ['HS256'],
},
log: console.log.bind(console),
time: Date.now.bind(Date),
});
const token = await jwt.sign({ my: 'payload' });
- initJWT(services) ⇒
Promise.<Object>
- ~sign(payload, [algorithm]) ⇒
Promise.<String>
- ~verify([token]) ⇒
Promise.<Object>
- ~sign(payload, [algorithm]) ⇒
Sign the given payload
Kind: inner method of initJWT
Returns: Promise.<String>
- A promise to be resolved with the signed token.
Param | Type | Description |
---|---|---|
payload | Object |
The payload to sign |
[algorithm] | String |
The signing algorithm |
Example
const token = await jwt.sign({ my: 'payload' });
Verify and decode the given token
Kind: inner method of initJWT
Returns: Promise.<Object>
- A promise to be resolved with the token payload.
Param | Type | Description |
---|---|---|
[token] | String |
The token to decode |
Example
const payload = await jwt.decode('my.jwt.token');