ForrestJS service which helps handling jwt activities. More or less it provides
a Promise wrapped jsonwebtoken
module.
service-jwt
decorates the App's Context with a jwt.sign()
and jwt.verify()
helpers:
registerAction({
target: 'myHook',
handler: async (args, ctx) => {
const token = await ctx.jwt.sign({ foo: 123 });
console.log(token);
const data = await ctx.jwt.verify(token);
console.log(data);
},
});
After the App initializes, you can simply import the helpers and use it straight:
const { sign, verify } = require('@forrestjs/service-jwt');
const token = await sign({ foo: 123 });
const isValid = await verify(token);
NOTE: it is important to list service-jwt
AFTER service-env
so that any kind of environment
configuration is available to be used.
JWT_SECRET=xxx
JWT_DURATION=1y
registerAction({
target: '$SETTINGS',
handler: ({ setConfig }) => {
setConfig('jwt.secret', 'your-safe-secret');
setConfig('jwt.duration', '1y');
},
});
settings
can use whatever you would pass to jsonwebtoken
. The property
expiresIn
is defaulted to the global duration
setting.
customSecret
is defaulted to the global secret
setting.
customSecret
is defaulted to the global secret
setting.
It resolves with the token's decoded content or throws an error in case it it not valid.