1.0.0 • Published 6 years ago
@dsninjas/jwt v1.0.0
ds-util-jwt
Jwt Helpers for node.js projects
This module makes it easy to issue and verify tokens in node.js applications.
Installation
- Make sure that Node.js installed.
- Install Node.js library with
npm
:
npm install @dsninjas/jwt
- You can also install Node.js library with
yarn
:
yarn add @dsninjas/jwt
Usage
constructor(secret, audience, issuer)
The constructor helps you configure the jwt helper class.
const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const jwtHelper = new JwtHelper(secret, audience, issuer);
The constructor takes the following options:
- secret (required): this is the secret used to create the .
- audience (required): this is used to check the audience.
- issuer (required): this is the isser of the token.
issue(payload, expirytime, subject, cb)
This method helps you to generate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.
With promise:
const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
const result = jwtHelper.issue({ message }, expireIn);
result.then((result) => {
console.log(result);
}.catch((err) => {
console.log(err);
});
With callback:
const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
jwtHelper.issue({ message }, expireIn,'jwt-auth-token',(err,result) => {
if (err) {
console.log(err);
}
console.log(result);
}
- payload (required): this is the data you want to encrypt.
- expirytime (required): this is time the token woould expire.
- subject (optional): this is the subject of the jwt.
- cb (optional): this is the callback the woulld be called with the response.
Sample response:
{
uuid: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U'
}
verify(token, cb)
This method helps you to validate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.
With promise:
const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';
const result = jwtHelper.verify(token, expireIn);
result.then((result) => {
console.log(result);
}.catch((err) => {
console.log(err);
});
With callback:
const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';
const result = jwtHelper.verify(token, expireIn,'jwt-auth-token',(err,result) => {
if (err) {
console.log(err);
}
console.log(result);
}
- token (required): this is the data you want to encrypt.
- cb (optional): this is the callback the woulld be called with the response.
Sample response:
{
message: '6c$5M',
iat: 1516195754,
exp: 1516800554,
aud: 'http://nun.vc/simwi',
iss: 'http://nun.vc/simwi',
sub: 'jwt-auth-token',
jti: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14'
}
1.0.0
6 years ago