Zero dependency typescript friendly Node.js library designed to hash/decode JSON Web Tokens using symmetric/asymmetric algorithms
npm i @mokuteki/jwt
- Pick one hashing strategy out of existing ones
- Pass the strategy to JSONWebToken constructor
- Use created instance to generate/verify JWTs
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');
const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});
const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };
const token = jwt.generate(payload);
const decoded = jwt.verify(token);
console.log(decoded); // { id: 1 }
const { JSONWebToken, RS256Strategy } = require('@mokuteki/jwt');
const strategy = new RS256Strategy({
ttl: 10000,
publicKey: 'YOUR_PUBLIC_KEY',
privateKey: 'YOUR_PRIVATE_KEY',
});
const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };
const token = jwt.generate(payload);
const decoded = jwt.verify(token);
console.log(decoded); // { id: 1 }
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');
const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});
const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };
/**
* Override JWT ttl option
*/
const token = jwt.generate(payload, { ttl: 1000 });
setTimeout(() => {
try {
const decoded = jwt.verify(token);
} catch (err) {
// err instanceof JwtExpiredError
}
}, 2000);
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');
const strategy = new HS256Strategy({
ttl: 10000,
secret: 'YOUR_SECRET',
});
const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };
const token = jwt.generate(payload, { ttl: 20000, secret: 'NEW_SECRET' });
try {
const decoded = jwt.verify(token);
} catch (err) {
// err instanceof InvalidSignatureError
}
Licensed under MIT